0

I'm doing an small Huffman compression-encryption program, and during the decompression of the file, my current getFile method (that should return the whole file as a String) ends sooner than expected, and trying to force it to read after a false-positive EOF (maybe? It's consistent with the same string) causes the program to crash.

Here's my current method:

string getFile(string route){
    ifstream reader; 
    string s=""; 
    reader.open(route); 
    if(reader.bad())return "FAILURE TO OPEN FILE"; 
    reader.read((char*)&dictionary.weight, sizeof(int)); // There's an int in the beggining
    // It describes the original ammount of chars there were in the original file. 
    while(!reader.eof()){ s+= reader.get() } 
    reader.close(); 
    return s; 
}

1 Answers1

2

You should open the file in binary mode. This means that no translation should be performed between the contents of the file, and what your program sees. The default is called text mode and various transformations may occur there; e.g. in Windows typically \r\n is translated to \n, and byte 26 may appear to be end-of-file.

reader.open(route, ios::binary);

Also, don't use eof in a loop condition

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365