Thread: Mac - Default Lines Endings - fgets() - no worky

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    Mac - Default Lines Endings - fgets() - no worky

    I'm on a Mac running Tiger. I use TextWrangler for basic editing of text files and scripts. I created a simple 2 line text file and saved it with TextWrangler's Text Files encodings set up for Windows (CRLF).

    My C program does a fgets(). In the Xcode debugger, I see that the first line has a \r\n as the line endings, as does the second line also on the next fgets().

    Then, I changed TextWrangler's preferences to save files in "Mac format", which is just CR. Xcode now tells me the line end character is a \r, and if I dump the file I see a single 0x0D.

    However, now fgets() does not respect the single 0x0D as the newline character, and reads to the end of the (very short, two line) file.

    What have I done wrong? Why wouldn't fgets() treat the 0x0D as a newline character?

    Todd

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's the dump of the file when saved in "Mac" format:
    Code:
    OFFSET:
    000000  49206C69 6B652070 726F6772 616D6D69  6E670D70 726F6772 616D6D69 6E672069   *I like programming.programming i*
    000020  73206675 6E0D                                                              *s fun.*

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's the same file saved as 'Windows" format:
    Code:
    OFFSET:
    000000  49206C69 6B652070 726F6772 616D6D69  6E670D0A 70726F67 72616D6D 696E6720   *I like programming..programming *
    000020  69732066 756E0D0A                                                          *is fun..*

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Todd Burch View Post
    What have I done wrong? Why wouldn't fgets() treat the 0x0D as a newline character?
    How would it know which way to treat the file? Perhaps there is some non-standard mode character to fopen() which tells it "Mac-style line terminators."

    You could react to this issue by writing a generalized fgets()-like routine, which accepts \r, \n, or \r\n as line terminators, not caring which is which.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's the same file again, saved with the "UNIX (LF)" format (Line Feed):
    Code:
    OFFSET:
    000000  49206C69 6B652070 726F6772 616D6D69  6E670A70 726F6772 616D6D69 6E672069   *I like programming.programming i*
    000020  73206675 6E0A                                                              *s fun.*
    Running in the debugger, now only the first line is read. So, it appears that 0x0A is what fgets() is looking for.

    Humm...

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Todd Burch View Post
    Running in the debugger, now only the first line is read. So, it appears that 0x0A is what fgets() is looking for.

    Humm...
    Well, ever since going to Darwin, Mac has become much more "UNIX-like". There is probably some Mac-specific function which reads lines using Mac-style line terminators, and fgets() assumes UNIX line terminators.

    Irritating but not mysterious, I think.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    More research done.

    The "Mac" format of just using CR is OS 9 and prior. Therefore, in my book, ancient history. Won't be using that any more. Problem solved!

    Thanks, Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  4. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM