getline- to limit input (where do the unused characters in stdin go?)

Discussion in 'C Programming' started by Luke Wu, Jan 26, 2005.

  1. Luke Wu

    Luke Wu Guest

    I have been using the getline function from K&R2 for simple character
    input.
    The prototype is

    int getline(char *s, int limit); /* returns 0 to signal EOF */

    This function simply calls getchar() in a loop, which breaks if EOF or
    too much input occurs (and places necessary '\0').

    I always made sure the buffer pointed to by s was large (corresponding
    to large limit argument). I only did this thinking that if the user
    typed in a lot, then those characters that were not taken in (because
    the limit has been reached), then those characters will be sitting in
    my input stream waiting to attack the next naive character input.

    Today I tested this fear, and it wasn't true. The remaining characters
    disappeared when I typed in a lot of junk...

    ..#include <stdio.h>

    ..#define MAXLINE 4

    ..int getline(char line[], int max);

    ..int main()
    ..{
    .. int length;
    .. char line[MAXLINE];
    ..
    .. while((length = getline(line,MAXLINE)) > 0)
    .. {
    .. puts(line);
    .. }
    ..
    .. system("PAUSE");
    ..
    .. return 0;
    ..}


    Sample input/output (my console):

    input echo: 12345678
    output: 123
    input echo: 12345678
    output: 123


    What I was expecting (which always made me set MAXLINE very high):

    input echo: 12345678
    output: 123
    input echo: 12345678
    output: 456

    (with 7812345678 still waiting to be read thereafter).



    Is there something I am getting wrong? Can I get over my fear and set
    MAXLINE to a reasonable value from now on? (Already read the FAQ)
    Advanced Thx
    Luke Wu, Jan 26, 2005
    #1
    1. Advertising

  2. Luke Wu

    S.Tobias Guest

    Luke Wu <> wrote:
    > I have been using the getline function from K&R2 for simple character
    > input.
    > The prototype is


    > int getline(char *s, int limit); /* returns 0 to signal EOF */


    For others' convenience:
    /* getline: read a line into s, return length */
    int getline(char s[],int lim)
    {
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
    s = c;
    if (c == '\n') {
    s = c;
    ++i;
    }
    s = '\0';
    return i;
    }

    [snip]

    > .#include <stdio.h>


    > .#define MAXLINE 4


    > .int getline(char line[], int max);


    > .int main()
    > .{
    > . int length;
    > . char line[MAXLINE];
    > .
    > . while((length = getline(line,MAXLINE)) > 0)
    > . {
    > . puts(line);
    > . }
    > .
    > . system("PAUSE");
    > .
    > . return 0;
    > .}



    > Sample input/output (my console):


    > input echo: 12345678
    > output: 123
    > input echo: 12345678
    > output: 123



    > What I was expecting (which always made me set MAXLINE very high):


    > input echo: 12345678
    > output: 123
    > input echo: 12345678
    > output: 456


    > (with 7812345678 still waiting to be read thereafter).


    This expectation is incorrect, see below.

    > Is there something I am getting wrong? Can I get over my fear and set
    > MAXLINE to a reasonable value from now on? (Already read the FAQ)
    > Advanced Thx


    I have no idea what happens in your case. For reference this is my
    output, which is as expected:
    $ ./a.out
    12345678 [input echo]
    123
    456
    78

    [^D]
    $

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jan 27, 2005
    #2
    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. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,844
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    771
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,328
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    420
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    286
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page