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

L

Luke Wu

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
 
S

S.Tobias

Luke Wu said:
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]
$
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top