#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    32
    Rep Power
    13

    removing trailing newline in C


    currently i'm using fgets() to get a line from the standard input stream (stdin)... but i do not want the trailing newline that fgets() returns to me together with the line... how do i go about removing that trailing newline?

    what i'm currently doing is iterating over every character in the character array and, if it's a '\n' or '\r' character, i replace it with the terminating null character '\0'
    Code:
            /* strip trailing newline */
            for (i = 0; i < strlen(inputline); i++)
            {
                if ( inputline[i] == '\n' || inputline[i] == '\r' )
                    inputline[i] = '\0';
            }
    will this cause any problems? and is there a better way to do this?

    thanks
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Well, since fgets() reads until it either encounters a newline or until the buffer is full, there'll never be a newline hidden somewhere in the string, but only at the end:

    Code:
    fgets(buf, sizeof(buf), stream);
    if (stream[strlen(stream) - 1] == '\n') {
      stream[strlen(stream) - 1] == '\0';
    }
    If you also want to get rid of carriage returns, then you'd have a reason to scan the entire string.

    However, simply replacing every occurrence with a NUL-byte will get confusing. For instance, assume the buffer is 20 bytes large, and a 12-byte string is read, with a carriage return ('\r') byte at index 5 (the 6th character). By replacing it with '\0', you pretty much throw away the rest of the string, since there's no way to differentiate between the NUL byte inserted by fgets() and the NUL byte you replaced the '\r' with.

    So you should probably replace it with another character (a space, perhaps), or delete it by shifting all the following characters one place to the left.

    Just my $0.02 :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    You can also try out the fgetln() function (if it exists for your compiler), which will save you a call to strlen().
    http://www.openbsd.org/cgi-bin/man.c...penBSD+Current has more documentation
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    32
    Rep Power
    13
    thanks guys :)

    i'd seen the fgetln() function while looking thru the man pages before but it seemed to cause roughly the same problems since now i'd have to still remove the newline character (every input line comes with a terminating newline) and add the terminating NUL char

    i think i'll use Analyser's way
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    Another solution:

    char* nl;
    char mydata[80];

    fgets(mydata, 80, stdin)
    nl = strrchr(mydata, '\r');
    if (nl) *nl = '\0';
    nl = strrchr(mydata, '\n');
    if (nl) *nl = '\0';




    That will cream the trailing \r, and if there is a spurious \n before that, it'lll get creamed to. The other proposed solution looks faster.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo