Using the STL function to read the content of the file content

xiaoxiao2021-03-06  115

Yesterday, a friend reads the file content into a char array in a function in STL.

The method used is:

Ifstream Btfile ("1.txt");

Ostringstream Temp;

Temp << btfile;

Discover it to read 8 Byte.

So to:

Ifstream Btfile ("1.txt");

Ostringstream Temp;

Temp << btfile.rdbuf ();

This seems to be right, but a pair of comparison discovery is less than some characters. After a burst of debugging and research, it was found that 1.TXT contained some Chinese characters, all of which were all lost.

Finally, finally found the correct way:

Ifstream btfile ("1.txt", ios_base :: binary;

Ostringstream Temp;

Temp << btfile.rdbuf ();

That is to use the binary format, not the text format to read the file. This method is consistent with a series of functions to read file content with Fopen, etc., all of which open up a Buffer, regardless of the format in the file, and put it in the buffer every time you read a BYTE.

转载请注明原文地址:https://www.9cbs.com/read-103019.html

New Post(0)