Re: [gtkmm] Subject: possible bug in Gtk::Main kit



kmilo softhome net writes:

> the first time the function ayudar run fine, but the second time (after
> Gtk::Main   kit) ayudar print bad the file
>
> #include <gtkmm.h>
> #include <iostream>
> #include <fstream>
>
> void ayudar(){
>     char* buffer=NULL;
>     int tamanoBytesFname=0;
>     std::ifstream file ("help.txt");
>     file.seekg (0, std::ios::end);
>     tamanoBytesFname =  file.tellg();// obtiene el tamano del archivo
>     file.seekg (0, std::ios::beg);
>     buffer = new char [tamanoBytesFname];
>     file.read(buffer,tamanoBytesFname); // lee el archivo de ayuda
>     file.close();
>     std::cout << buffer << std::endl << std::endl;
>     delete[] buffer;
> }

Your function is buggy. The call to 'std::cout <<' expects buffer to 
be a null-terminated C string, which it is not. Try something like:

  buffer = new char [tamanoBytesFname + 1]; // extra space for '\0'
  file.read(buffer,tamanoBytesFname); // lee el archivo de ayuda
  file.close();
  buffer[tamanoBytesFname] = '\0';
  std::cout << buffer << std::endl << std::endl;

-- 
Ole Laursen
http://www.cs.auc.dk/~olau/



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