user3545752 user3545752 - 1 year ago 91
C++ Question

Checking line breaks (newline characters) with CFile

I need to check whether a file is ended with a line break or not, using

What I have tried:

  1. point the file pointer at the end of the file

  2. move the pointer back by 2 units

  3. check if the pointer is pointing at

Here is my code:

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
character and a garbage character (with weird values like 204). After some research through the documentation, I found that
only count
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download