0

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
}
3
Contributors
3
Replies
27
Views
3 Years
Discussion Span
Last Post by rubberman
0

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.

0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.