Handling User's Input

Discussion in 'C Programming' started by der, Aug 17, 2003.

  1. der

    der Guest

    Hello all,

    I want to use fgets() to read lines.
    Now, if the user has entered more characters than it was supposed to,
    the next call to fgets() would read
    these characters, which I don't want to be read, but rather
    reading fresh new line instead.

    How should I deal with such scenario?

    Here is my way of solving this problem.
    I would like to know what you think about it.

    void
    getline(char *s, int size)
    {
    int len;
    int correct_input;

    do {
    correct_input = 1;
    fgets(s, size, stdin);
    len = strlen(s);
    if (len == size-1 && s[len-1] != '\n') {
    /* if we have read size-1 characters, and the next
    * one was '\n', delete it from stdin's buffer, and
    * exit the loop.
    */
    if (getchar() == '\n')
    break;
    /* If we are here, we have read too many characters.*/
    correct_input = 0;
    while (getchar() != '\n')
    ;
    }
    if (!correct_input)
    printf("line is too big, please enter shorter"
    " line.\n");
    } while (!correct_input);
    }

    Thanks in advance,

    der
     
    der, Aug 17, 2003
    #1
    1. Advertising

  2. der

    der Guest

    Irrwahn Grausewitz wrote:

    > der <> wrote in <3f3fdaea@news.012.net.il>:
    >
    >>Hello all,
    >>
    >>I want to use fgets() to read lines.
    >>Now, if the user has entered more characters than it was supposed to,
    >>the next call to fgets() would read
    >>these characters, which I don't want to be read, but rather
    >>reading fresh new line instead.
    >>
    >>How should I deal with such scenario?
    >>
    >>Here is my way of solving this problem.
    >>I would like to know what you think about it.
    >>

    >
    > <CODE EXAMPLE SNIPPED>
    >
    > Your code seems to work (I haven't tested it thoroughly).
    >
    > I personally like another approach:
    >
    > - use a dynamically allocated input buffer, so you
    > can adjust it's size if necessary
    > - read input on a char-by-char basis with fgetc()
    > into your buffer
    >
    > This way you've got full control over input handling and dealing
    > with newline / EOF / error conditions is very easy. I admit that
    > basically this means you have to write your own fgets() funtion,
    > but besides being a good exercise you have the advantage that you
    > can easily change it to suit your needs (for example if you want
    > to split up input on certain characters received). And it may be
    > shorter as your code as well...
    >


    Thanks for pointing this out.
    I will use this strategy too.

    > Just be aware, that if you access the buffer via a static pointer
    > local to your function, you have to copy the result before the
    > next time your function is called!


    Yes, of course.

    Thanks for your help,

    der
     
    der, Aug 17, 2003
    #2
    1. Advertising

  3. On Sun, 17 Aug 2003, der wrote:

    > Hello all,
    >
    > I want to use fgets() to read lines.
    > Now, if the user has entered more characters than it was supposed to,
    > the next call to fgets() would read
    > these characters, which I don't want to be read, but rather
    > reading fresh new line instead.
    >
    > How should I deal with such scenario?


    Two ways come to mind. The first is to read in a fixed length and then
    toss away anything that exceeds that length for a given line. The second
    is to grow the buffer until it is big enough to hold whatever length the
    user input.

    For the first method, use an fgets to read what you want to keep. If the
    last character is not a '\n' then you know there is more to get. You then
    want a function that gets and discards the input until '\n' is reached.
    Something like:

    fgets the line you want
    while(more line left)
    fgets more into temporary buffer

    For the second solution I would use:

    allocate a buffer of size XXX
    fgets into &buffer[0]
    while(more line left)
    realloc buffer to XXX += XXX
    fgets into &buffer[XXX]

    So you just keep tacking on to the end of the newly resized buffer.

    > Here is my way of solving this problem.
    > I would like to know what you think about it.
    >
    > void
    > getline(char *s, int size)
    > {
    > int len;
    > int correct_input;
    >
    > do {
    > correct_input = 1;
    > fgets(s, size, stdin);
    > len = strlen(s);
    > if (len == size-1 && s[len-1] != '\n') {


    You could also search the string for a '\n'. If it does not exist then you
    have the same situation but it is only one comparison.

    > /* if we have read size-1 characters, and the next
    > * one was '\n', delete it from stdin's buffer, and
    > * exit the loop.
    > */
    > if (getchar() == '\n')
    > break;
    > /* If we are here, we have read too many characters.*/
    > correct_input = 0;
    > while (getchar() != '\n')
    > ;


    Getting one character at a time is not the most efficient way to clear the
    buffer. Since you are going to prompt the user for input again, just use:

    do {
    fgets(s, size, stdin);
    while(strchr(s, '\n') == NULL);

    > }
    > if (!correct_input)
    > printf("line is too big, please enter shorter"
    > " line.\n");
    > } while (!correct_input);
    > }
    >
    > Thanks in advance,
    >
    > der
    >


    --
    main(){int j=1234;char t[]=":mad:abcdefghijklmnopqrstuvwxyz.\n",*i=
    "iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
    *i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
     
    Darrell Grainger, Aug 18, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam
    Replies:
    12
    Views:
    675
    Steve Holden
    Oct 8, 2006
  2. Replies:
    4
    Views:
    686
    Walter Roberson
    Sep 9, 2005
  3. Replies:
    3
    Views:
    411
  4. Chris Carlen
    Replies:
    1
    Views:
    635
    Gabriel Genellina
    Sep 18, 2007
  5. mike
    Replies:
    5
    Views:
    99
    Ted Zlatanov
    Feb 6, 2008
Loading...

Share This Page