atoMerz
atoMerz

Reputation: 7672

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?

Upvotes: 1

Views: 1071

Answers (2)

Dan
Dan

Reputation: 13170

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);

Upvotes: 1

Pete Becker
Pete Becker

Reputation: 76417

On Windows, the representation of end-of-line in a text file is two bytes: 0x0d, 0x0a. When you use text mode to read from such a file, those two bytes get translated into the single character '\n'. When you use binary mode you're reading raw bytes, and they don't get translated for you. If you don't want them, you'll have to do the translation yourself.

Upvotes: 2

Related Questions