Jump to content

[SOLVED] Text file to string


photo

Recommended Posts

Posted

This solution is work. But I'm think that is not rational. I would some advises.

 

 

File file = new File(filename, "rb");
if(!file.isOpened())
log.error("can't open file %s\n",filename);
else
{
        	string sourcecode = "";
        	while(file.tell() < file.getSize())
        	sourcecode += format("%c",file.getc());
            log.message("body : \n%s\n",sourcecode);
            file.close();
}

Posted

You are opening your text file in binary mode ("rb"), try text mode instead ("r")

File file = new File(filename, "r");

 

Maybe you could also use File base class function Stream::gets() instead of character-wise reading.

Posted

Ulf is right, except for that "rb" file option is better than "r" because this is more portable across different OS. Windows skips '\r' symbol on reading and automatically adds a '\r' symbol after '\n' on writing with "w" opening mode.

×
×
  • Create New...