fgets - It will always return [string]\n

This is a discussion on fgets - It will always return [string]\n

Thread: fgets - It will always return [string]\n\0 ?

?
within the C Programming forums, part of the General Programming Boards category; Sorry. Im still a bit upset over input validation. Consider the following: Code: fgets(BUF,10,stdin); BUF is supposed to take in ...

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

    Question fgets - It will always return [string]\n\0 ?

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > 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
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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,006
    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. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21