Thread: gah newlines!

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    gah newlines!

    I thought if you open a file in text mode you can use '\n' which implicitly expands to the platform specific newline.

    the below code never compares '\n' to be true. What's the easiest way to do such a comparison?


    Code:
    FILE* istream = fopen("text.txt", "r"); 
    
    while ( EOF != (c = fgetc(istream)) ) {
         
         if ( '\n' == c ) { 
              puts("detected newline");
         }
    }
    Last edited by keira; 12-02-2007 at 12:29 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Generally a newline will be one of the following:

    • '\n' [*nix systems]
    • '\r''\n' [Windows]
    • '\r' [Mac]


    Not to be annoying, but are you sure test.txt has any '\n' chars? Are you sure that istream isn't NULL before the loop?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No idea, it works well enough here
    Code:
    $ cat foo.c
    #include <stdio.h>
    int main(void)
    {
        FILE *istream = fopen("text.txt", "r");
        int c;
        while (EOF != (c = fgetc(istream))) {
    
            if ('\n' == c) {
                puts("detected newline");
            }
        }
        return (0);
    }
    $ gcc foo.c
    $ cat text.txt
    hello
    world
    $ ./a.exe
    detected newline
    detected newline
    Did you get the right filename, in the right directory?
    Did you check for a successful fopen() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    I'm in win32 so its gotta be this '\r''\n' business MacGyver was talking about. Trust me everything else is good

    But how do I do a comparison for '\r''\n'? Before I wrote this post I was about to write a function that takes in the FILE object and first checks for 0x0d (which is a CR) and then seeks ahead to the next byte to check for 0x0A (LF). But I'm hoping for a better solution lol.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Searching for '\n' should still work since every '\r''\n' contains a '\n'.

    Again, I would check to make sure istream is not NULL before you start reading. That could very easily cause you to not get any newline chars to be registered.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not a \r\n issue here.
    Same code, different compiler.
    Code:
    > cl -nologo foo.c
    foo.c
    
    > type text.txt
    hello
    world
    
    > foo.exe
    detected newline
    detected newline
    Do exactly what I've done, and paste exactly what you see.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To detect \r\n, if you need it:

    Code:
    if (c == '\r' && fgetc(istream) == '\n') // newline detected
    Or a safer approach:
    Code:
    if (c == '\r')
    {
    	c = fgetc(istream);
    	if (c == '\n') // newline detected
    }

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    alright '\n' does work. the hexidecimal revealed that there were hidden 0x09 ('\t')s right before the '\n' which was causing the issue. Thanks all for help!

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    but... if you have a '\n' in a switch statement is it different from if ( '\n' == nextChar ) ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extra newlines in code boxes
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-08-2009, 04:41 PM
  2. Newlines
    By ntwrk_ovrflow in forum C Programming
    Replies: 4
    Last Post: 04-22-2009, 11:28 AM
  3. newlines and getc()... i just dont get it.
    By keira in forum C Programming
    Replies: 5
    Last Post: 12-05-2007, 12:30 AM
  4. Gah! More math!
    By BMJ in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-02-2002, 09:33 PM
  5. Gah.... leave me be......
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-15-2001, 09:23 PM