I am using fgets to read lines of a file. I am then using if statements based on the location of the new line character to decide which sscanf to use. I have been getting a lot of windows files which usually use carriage returns. Most of the timeit "seems" to work ok with carriage returns but other time it goes crazy on me. I was wondering how I should handle this. Should I add the ascii value of carriage returns to my if statements? Or is there a better way to handle this?

if ( line[10] == 10 )
{
    //sscanf
}

if ( line[20] == 10 )
{
    //sscanf
}

if ( line[30] == 10 )
{
    //sscanf
}

Recommended Answers

All 3 Replies

Line breaks in Windows files are a cr+lf (carriage-return + line-feed) pair of bytes. For Linux/Unix systems, it is a simple line-feed character. So, scan for a line-feed, and then inspect the immediately previous character to see if it is a carriage-return. If it is, then break there and throw away the cr+lf pair to continue processing. If not, then just throw away the lf, and continue processing after that. The common denominator is the line-feed.

It can get even more complicated than that -- for a complete discussion see this article. At one time the MAC used CR as line terminator, don't know if that' changed in recent years or not.

@AD
Good point, but since current MAC systems use BSD as their basic OS, I think they use the *nix LF terminator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.