Thread: fgets - It will always return [string] + newline + nul?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    41

    Question fgets - It will always return [string] + newline + nul?

    Sorry. Im still a bit upset over input validation.

    Consider the following:
    Code:
     fgets(BUF,10,stdin);
    BUF is supposed to take in 10 chars. But only 9 chars will be readable by us. Since the last char will always be a '\0'.

    Defination of fgets:
    Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first.
    A newline character ends reading but is considered a valid character and included in the new string.
    A null character is always appended at the end of the resulting string.
    So there are 3 conditions to monitor:

    1. A user keys in exactly (num-1) chars.
    2. A user keys in more than (num-1) chars.
    3. A user keys in less than (num.1) chars.

    Condition 1:
    If a user keys in the following:

    TAEKWONDO

    and hits a carriage return; what is actually being taken into the system is:

    TAEKWONDO\n\0

    So BUF will be TAEKWONDO\0 ? Since BUF has already read (num-1) chars? What happened to the \n then?
    Condition 2:
    But if a user keys in the following:

    TAEKWONDOTAEKWONDOTAEKWONDO

    and hits a carriage return; what is being captured by the system is:

    TAEKWONDOTAEKWONDOTAEKWONDO\n\0

    So BUF will be TAEKWONDO\0 also? The remaining chars will be stored in the buffer?
    Condition 3:

    If a user keys in the following:

    HELLO

    and hits a carriage return; what is being captured by the system is:

    HELLO\n\0

    BUF will be exactly HELLO\n\0 ?
    Im getting confused. Can someone enlighten please? Thanks
    If I'm able to turn back time, I would learn C as my first language.

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    try putting this into a program, and experimenting with it:

    Code:
    fgets(buffer, 10, stdin);
    printf("%s", buffer);
    you'll notice that you're right on all conditions.

    however, on your first condition, you say the user keys in exactly num-1 characters, but thats not correct seeing as how the \n is a character. So, you're actually entering 10 characters there, and fgets will take off the last character in order to put its null character in

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What happened to the \n then?
    > The remaining chars will be stored in the buffer?
    You'll get them on the next fgets() call.
    Basically, if you don't find a \n, then expect a long line and deal with it accordingly.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Oh ok. Thanks! I shall play with it again.

    But about backspaces '\b'?

    Are \b being taken in also? And what about tabs '\t' ?

    Are they considered to be one character also?
    If I'm able to turn back time, I would learn C as my first language.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by stevong
    Oh ok. Thanks! I shall play with it again.

    But about backspaces '\b'?

    Are \b being taken in also? And what about tabs '\t' ?

    Are they considered to be one character also?
    Yes, yes, yes, yes, respectively.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Quote Originally Posted by Salem
    > What happened to the \n then?
    > The remaining chars will be stored in the buffer?
    You'll get them on the next fgets() call.
    Basically, if you don't find a \n, then expect a long line and deal with it accordingly.
    oh ok..Hmm...I just wrote and tested this and it seems fine in my outputs.
    it managed to clear unwanted buffer (the extra buffer that exceeded the desire char length) and the irritating \n at end of the input (if any)

    Is this foolproof enough?

    Please advise.

    Code:
             fgets(buf,10,stdin);
             if(buf[strlen(buf)-1]!='\n')
                while((ch=getchar())!='\n' && ch!=EOF); 
             else
                buf[strlen(buf)-1]='\0';
    If I'm able to turn back time, I would learn C as my first language.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, if you want to do that, then that would be ok

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by stevong
    Code:
             fgets(buf,10,stdin);
             if(buf[strlen(buf)-1]!='\n')
                while((ch=getchar())!='\n' && ch!=EOF); 
             else
                buf[strlen(buf)-1]='\0';
    You don't really need to walk the string twice. You could capture the result of strlen in a variable and use it again, like in the foo, here.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-15-2015, 06:18 PM
  2. function to remove newline character (fgets)
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 04-29-2012, 09:48 AM
  3. fgets with newline in the middle of the stdin
    By sebby64 in forum C Programming
    Replies: 2
    Last Post: 11-27-2010, 01:27 PM
  4. fgets moving on to a newline
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 11-05-2002, 02:57 PM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM