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?