int size; char *fileData; ifstream file("big.txt"); file.seekg(0, ios::end); size = file.tellg(); file.seekg(0, ios::beg); fileData = new char [size]; file.read(fileData,size); file.close(); cout << fileData << endl; 

Why fileData write garbage at the end of the line ( size calculated correctly)?

    1 answer 1

    Try

     ifstream file("big.txt",ios::binary); ... fileData = new char [size+1]; file.read(fileData,size); fileData[size]=0; 
    • Garbage is gone, but now adds HHHH instead of it - Vadim Moroz
    • Well, open the file as a binary ... - Harry
    • why is it a text file? - Vadim Moroz
    • Then, since it is a text, you \n\r will be converted to ONE character, and for tellg , it will be counted as two. The difference is trash-filled. I thought you were working in Linux, there is only one symbol, but it seems that you are in Windows, and here it is important. - Harry