Thread: fgets - user input including spaces

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>What if I really wanted to enter 1 followed by 500 zeros?
    Try it and see, just like any good programmer

    >>Do you think I could actually do it?
    Der, yeah!

    >>How should the program handle it?
    fgets() will read as much as it can, and the rest will remain in the input buffer, waiting for the next read.

    >>Is this a valid number?
    Depends on your definition of valid. 21 isn't valid if you only accept 1 through 19.

    >>Should all programs check validity of input
    Wherever practicle, yes, do your best. For some programs, input validation is essential, for example a server that is public-facing. A buffer overflow is the source of many worms But then, a small program that is only going to be run locally (by yourself, say) might not require a bullet proof interface.


    In this code:
    Code:
    char buf[BUFSIZ] = {0};
    fgets( buf, BUFSIZ, stdin );
    if( strchr( buf, '\n' ) == NULL )
    {
        ...more than BUFSIZ entered, deal with it...
    }
    ... a lack of \n does not necessarily mean there's more data to come (ie full data load). This could simply be the last line in the file, that doesn't actually contain a \n character on it.

    >>Is the following legal?
    >>char buf[len];
    Yes, in C99. No, not in C89.

    There's more info on reading numbers here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    and a short note on reading strings here
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    In this code:
    Code:
    char buf[BUFSIZ] = {0};
    fgets( buf, BUFSIZ, stdin );
    if( strchr( buf, '\n' ) == NULL )
    {
        ...more than BUFSIZ entered, deal with it...
    }
    ... a lack of \n does not necessarily mean there's more data to come (ie full data load). This could simply be the last line in the file, that doesn't actually contain a \n character on it.
    It does if it's from the keyboard. If it's a piped file, then you're right. However, the portion of "deal with it..." would successfully handle this anyway.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>It does if it's from the keyboard.
    Not necessarily. You can still enter EOF from there (CTRL+Z for example)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    >>It does if it's from the keyboard.
    Not necessarily. You can still enter EOF from there (CTRL+Z for example)
    True, but none of these people know how to do that...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    The purpose of my unanswered questions was to get people to try different things, and, in some cases, even to think about things. (Is 10 to the 500th power representable by any C or C++ built-in data type? Why bother with trying to read a 500-digit number? Maybe print a polite message and abort?)

    It's OK to ask other people to help you understand things, but maybe better if you poke around on your own for a while.

    My work here is done.

    I really enjoy hearing from the varied points of view represented here.


    "I was born not knowing, and have only had a little time to change that, here and there."
    --- Richard P. Feynman

  6. #21
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I am clear now on the use of fgets - in all the effort to encourage the use of that function as opposed to using scanf, it slipped by my notice that one can end up with characters being left in the input stream, much like you can with scanf. As Quzah mentioned, the programmer has to deal with it. So then, what is to be done with the 'extra' input? In some of the threads I was reading today, I saw that it is not necessarily good to just get rid of the extra input. Instead it should be dealt with in an intelligent manner. On the other hand, if the programmer was reasonably sure that the extra characters would not be necessary, why not just get rid of them? That way the next time input is required, the old input is not just waiting there to mess with the results.

    The point to all of this is that fgets does not seem to be such a great function to use after all. I mean, sure there is more safetly in using it, but its a bit of a pain too, and would it not be easier to write a getline function similar to the one you would find in K&R2, or that Steve Summit uses? Both utilise the getchar function, and impose restrictions on input length (more so in Summit's example) - if we have to go to this much trouble, why even say fgets is a good solution ? (I am sure I am missing something here, hence the reason for the question).

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>would it not be easier to write a getline function
    But fgets() is a get line function.

    Steve's example automatically removes characters that are too long to fit in the buffer. The problem is, it does it siliently, the function invoking getline() will never know that it has lost data. This might be acceptable in some cases but not in others.

    Ultimately it comes down to the requirements for that specific program and circumstance.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #23
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    What if I really wanted to enter 1 followed by 500 zeros?
    I got to thinking and did this varient of that question.
    Code:
    #include <stdio.h>
    
    int main(void){
            char buffer[3]; /*an unreasonably low*/
                                /*number for a name*/
            printf("Enter your name:  ");
    
            fflush(stdout);
            fgets(buffer,sizeof buffer,stdin);
            printf("%s",buffer);
            while((fgets(buffer,sizeof buffer,stdin))!=NULL)
                    printf("%s",buffer);
            putchar('\n');
            return 0;
    }
    Is this a good way to do that, and also, why does it hang after printing the name?

  9. #24
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>why does it hang after printing the name?
    It doesn't "hang", it's waiting for your input. You told it to loop until fgets() returns NULL, which will only happen when there's no more data to be had, either through EOF or a read error.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #25
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    thanx I just realized that, I feel so stupid

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't fgets() read my input?
    By laczfinador in forum C Programming
    Replies: 10
    Last Post: 05-13-2009, 04:20 AM
  2. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM