Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Joda

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

This topic is 5633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
my parser just ignores lines(and \r and \n) completely, but it all depends on what you''re doing

Share this post


Link to post
Share on other sites
If you open your file handle in text mode, ie fopen(filename, "tr"), then all line endings regardless of OS are converted to ''\n''.

Share this post


Link to post
Share on other sites
You can convert the file with dos2unix, or a simple script.
The "t" mode in fopen isn''t guaranteed to exist.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!