UNIX: Handling win linefeeds in a unix app! PANIC!

Started by
3 comments, last by Joda 21 years, 3 months ago
Hey! My app is working with dat files for input. Im reading one float value from every line from my input files. Example Code: do { c = fgets(oneword, 100, fp1); /* get one line from the file */ sscanf( oneword, "%f", &x ); if (c != NULL) printf("%1.2f\n", x ); i++; } while (c != NULL); This works fine as long as I get files from UNIX users but when I get files from win32 users it wont work. I think it has something to do with incorrect linebreaks. Heard that win is using \r\n to mark linebreaks which is strange! So how do I replace "incorrect" linebreaks in my files autmatically? is there any open-source app or something? I triede dos2unix but it wont work... Thanks - Joda
Advertisement
Different OS''s have different line terminaters. Write your own version of fgets. For DOS/Windows lines end with \r\n. If I remember correctly Unix ends lines with just \n and Macintosh with just \r.

When I''ve done this in the past the routine I wrote sucked up all chars until it hit a \r or \n. If it was \r then it peeked ahead to see if the next char was a \n and grabbed that to.
-Mike
my parser just ignores lines(and \r and \n) completely, but it all depends on what you''re doing
If you open your file handle in text mode, ie fopen(filename, "tr"), then all line endings regardless of OS are converted to ''\n''.
You can convert the file with dos2unix, or a simple script.
The "t" mode in fopen isn''t guaranteed to exist.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement