Search code examples
c++filefstream

rdbuf() reading junk


Using this code I'm read a string from file.

pbuf = infile.rdbuf();
size = pbuf->pubseekoff(0, ios::end, ios::in);
pbuf->pubseekpos (0,ios::in);
buf = new char[size];
pbuf->sgetn(buf, size);
str.assign(buf, buf+size);

I have to read data in temporary variable char* buff since sgetn needs a char* not a string.
So at this point before asking my actual question if anyone knows a better way of reading a string from a file that may contain white space character please tell(Not looping till eof).

The content of the file is:
blah blah blah
blah blah in a new line

But what I get is:
blah blah blah
blah blah in a new line═

Playing around with the code I noticed the number of strange characters increases, as I add more \n characters. It seems when I try to get size of file each \n character takes 2 bytes of space, but when in a string it only takes 1 byte, and thus my string looks strange. How do I avoid this?


Solution

  • This is due to the standard library implementation turning the standard windows line ending \r\n into the standard c++ line ending \n.

    As @ipc says, you can use this answer to do what you want. (Note: According to the comments, the accepted answer on that question is not actually the best way to do it.)

    Alternatively, you can disable the line ending translation by opening the stream in binary mode, like so:

    std::ifstream t(fileName, std::ios_base::in | std::ios_base::binary);