getline() special...
-
Hey,
I found something special in the getline() function, I don't understand.
If I use the getline function in case of an Excel-sheet or an image - to get the image-data - the getline function breakes after a NULL termination ( \0 )
fstream oFile_e;
oFile_e.open("BAB.xls",ios::in);
oFile_e.getline(chTest, 100,'\n');
BAB.xls containes the \0 from position 9 to 12
chTest is after the getline function only filled up to position 9 ( after the first \0 )
--> no problem with the ios::binary mode. With this mode it's possible to read the whole data.
But if I write out some chars in a file and some of them are \0, I can read the whole data WITHOUT the ios::binary mode.
Do you understand that phenomenon?
-
getline() still reads the whole line of your file, but you use it as a C-string (char-array) afterwards. And from C's point of view, the character '\0' marks the end of a string, so any access through C functions would only result in the part before the first '\0' mark.
The best solution would be using the class std::string an the global function getline() instead of the istream member. Or you could use the method 'gcount()' to find out the real length of the read string.
-
thx for the answer but I think you don't understand what I mean. If i use the ios::binary mode in the getline function it works, too. But I don't understand why this funcion breakes ( stops ) reading when I read out image data, and an other time it continues ( when I write the data myself into an file with \0 ).
for example:
the image data: abcde\0\0\0\0cdf\0\0a
the data I wrote into a file: abc\0\0\0anow I read out both data with the same function and the same values / attributes.
So why fills the getline function my array compleately to the last char ( a ) at my own data and not only to the first \0 mark like in the image data?
Both files containes \0. One time I have an return of the function ( own data in an array: abc\0\0\0a ), one time not ( Image data in an array: abcde\0\0\0\0cdf\0\0a ).
So look... I don't have any problems to read out any data from an file, but im interested in this phenomenon.