Re: Problems reading from IOChannel



On Thursday 14 September 2006 11:27, Javier Aranega wrote:
> Hi !!
>
> The problem was a codification problem when reading the Glib::IOChannel
> codification from a file that was codificate with ISO-8859-15, extensible
> too for reading this from a pipe with Glib::IOChannel. The solution was
> very simple, with the set_codification(Glib::ustring) method of IOChannel:
>
> using namespace Glib;
>
> RefPtr<IOChannel> io;
> io = IOChannel::create_from_file(filename, "r");
> io->set_encoding("ISO-8859-15");
>
> Now it works fine. Perhaps this helps anyone.
>
> Javi Aránega
>
>> *Case 1: Trying to read a text file

[snip]

> > The file that I try to read is a text file with ascii characters.

[snip]

> > *Case 2: Using IOChannel with pipes to send/receive text between
> > This code work well in the most of the cases, but sometimes I get this
> > exception, I guess that could be some problem with the characters that I
> > read, but I don't understand because sometimes works sometimes I get the
> > exception if I work with normal characters (ascii). I have tried some
> > validate methods, but it doesn't work. How I could know what really
> > causes the problem? Someone have idea of how to fix that problem? Any
> > suggestion?

I was wondering whether to respond to this when you first posted, but as you 
said in both cases it occurred with ASCII characters, which comprise valid 
UTF-8 (and as it happens, also valid ISO-8859-15) I assumed it must be 
something else with your code.  You must have been putting non-ASCII 
characters, with a value greater than 127, into the Glib::IOChannel object.

You will get conversion exceptions if you try to put characters into a 
Glib::IOChannel object which don't match the current encoding (which by 
default is UTF-8).

By the way if your use of Glib::IOChannel is codeset neutral, then you may be 
better off setting the encoding to "" (if you do this no codeset conversion 
will be attempted, so any stream of bytes, including binary, will be 
accepted).  Hardcoded codeset conversion as you have done it can be quite 
dangerous (particularly if different users may be using different codesets).  
Depending on the usage you may want to get the user's locale programmatically 
and use that in the call to Glib::IOChannel::set_encoding().

Chris




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]