Re: Problems reading from IOChannel



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


2006/9/11, Javier Aranega <javi aranega gmail com>:
Hi!

I have a problem using Glib::IOChannel in my application, sometimes when I try to read from de IOChannel I have this exception (ILLEGAL_SEQUENCE)

glibmm-CRITICAL **:
unhandled exception (type Glib::Error) in signal handler:
domain: g_convert_error
code  : 1
what  : Hay una secuencia de bytes no válida en la entrada de conversión

I use IOChannel like this:

*Case 1: Trying to read a text file

using namespace Glib;
   
    IOStatus estado;
    ustring linea;
    ustring textomodelo;
    RefPtr<IOChannel> io;
   
    try { 
        io = IOChannel::create_from_file(parametros.getCadenaFicheroModelo(), "r");
        estado = IO_STATUS_NORMAL;
       
        textomodelo = "";
        while (estado!=IO_STATUS_EOF){
            estado = io->read_line(linea);
            textomodelo+=linea;
        } 
    } catch (FileError e){
        std::cout<<"No existe el fichero de modelo"<<std::endl;
    }

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

*Case 2: Using IOChannel with pipes to send/receive text between processes, in this case sometimes I get the BROKEN_PIPE exception

   channelMidi2Som = Glib::IOChannel::create_from_fd(pipe_m2s_padre[0]);
   channelSalida = Glib::IOChannel::create_from_fd(pipe_padre_clas[1]);
   channelClasificador = Glib::IOChannel::create_from_fd(pipe_clas_padre[0]);
           
   //Recibo, almaceno y reenvio los datos de m2s
   while (estado!=Glib::IO_STATUS_EOF){
      estado = channelMidi2Som->read_line(linea);
      if (estado!=Glib::IO_STATUS_EOF) {
          almacenaMuestrasm2s(linea);
          channelSalida->write(linea);
          //std::cout<<"Linea m2s: "<<linea<<std::endl;
      }
      resultadoMidi2Som+=linea;
  }
           
  channelSalida->close(true);
  channelMidi2Som->close(true);
               
  wait(NULL);

   //Recibo y almaceno los datos del clasificador
   estado = Glib::IO_STATUS_NORMAL;
   while (estado!=Glib::IO_STATUS_EOF){
      estado = channelClasificador->read_line(linea);
       if (estado!=Glib::IO_STATUS_EOF) {
            almacenaMuestrasClasificador(linea, parametros.getTipoClasificador());
            std::cout<<"Linea Clasificador: "<<linea<<std::endl;
       }
       resultadoClasificador+=linea;
   }
           
    channelClasificador->close(true);


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?
 
Thanks in advance.

Javi Aránega
U. de Alicante



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