I need to check whether a file is ended with a line break or not, using CFile
.
What I have tried:
\r\n
Here is my code:
cfile.SeekToEnd();
cfile.Seek(-2, CFile::current);
char buffer[2];
cfile.Read(buffer, 2);
if(buffer[0] == '\r' && buffer[1] == '\n') printf("Ended with line break!");
else printf("Not ended with line break!");
However, what I found is that the buffer gives me a \n
character and a garbage character (with weird values like 204). After some research through the documentation, I found that CFile::Read
only count \r\n
as a single character:
For text-mode files, carriage return–linefeed pairs are counted as single characters.
I am so confused because the file pointer obviously still counts 2 characters but I cannot get both of them. Is there any method to check line break at the end of a file with CFile?
I solved the problem by using CFile::typeBinary
when opening a file.
It seems that CFile::typeText
do something special processing carriage return–linefeed pairs (e.g. filling in a garbage character which is supposed to be a carriage return) which is not desired in my case.