Handling User's Input

D

der

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
 
D

der

Irrwahn said:
<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
 
D

Darrell Grainger

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);
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top