4

I'm facing a little problem with fgets when the input string exceeds its predefined limit.

Taking the example below:

    for(index = 0; index < max; index++)
    {printf(" Enter the %d string : ",index+1)
                if(fgets(input,MAXLEN,stdin))
                {
                    printf(" The string and size of the string is %s and %d \n",input,strlen(input) + 1);
                    removeNewLine(input);
                    if(strcmp(input,"end") != 0)
                   { //Do something with input
                   }
                }

Now when I exceed the length MAXLEN and enter a string, I know that the input will append a '\0' at MAXLEN -1 and that would be it. The problem happens when I try to enter the 2nd string which is not asked for i.e

Output :
Enter the first string : Aaaaaaaaaaaaaaaaaaaa //Exceeds limit
Enter the second string : Enter the third string : ....Waits input

So, I thought I should clear the buffer the standard way as in C. It waits until I enter

return

two times, The first time it being appended to the string and the next time,expecting more input with another return. 1. Is there any method by which I can clear the buffer without entering the extra return? 2. How do I implement error handling for the same? Because the fgets return value will be Non-null and strlen(input) gives me the accepted size of the string by fgets, what should be done?

Thanks a lot

Hooli
  • 711
  • 2
  • 13
  • 24

2 Answers2

4

If I understood correctly, looks like you want to avoid twice enter press, when entered input is within range.

A work around would be

for(index = 0; index < max; index++)
{
    printf(" Enter the %d th string :",index);
    // if (strlen(input) >=MAXLEN )

    if(fgets(input,MAXLEN,stdin))
    {

        removeNewLine(input);

        if(strcmp(input,"end") != 0)
        // Do something with input 
          ;
    }
    if (strlen(input) == MAXLEN-1 )
      while((ch = getchar())!='\n'  && ch != EOF  );

 }

With a limitation that it will again ask for two times enter when entered characters are exactly MAXLEN-2.

Or else you can simply form your input using character by character input.

P0W
  • 46,614
  • 9
  • 72
  • 119
  • Worked for me!Can you please explain the limitation part? – Hooli Aug 21 '13 at 07:08
  • @IDK, well say your `MAXLEN` is 10 , if you enter `abcdefgh` and `ENTER` it will again wait for another `ENTER` – P0W Aug 21 '13 at 07:25
  • It is not and why should it? I entered a string and if it's string length is equal to 9 excluding the null character ofcourse,it's possible that the string overflowed. Any code dependant issue here related to '\n' which is why its not waiting? Refer to the code above. Thanks! – Hooli Aug 21 '13 at 07:34
  • 1
    Well, for your case `fflush(stdin)` should just work fine. But its use is highly discouraged as it may lead to UB on some C implementation. **`while((ch = getchar())!='\n' && ch != EOF );`** is used instead. – P0W Aug 21 '13 at 07:40
3
while ((c=getchar()) != '\n' && c != EOF)
    ;

or:

scanf("%*[^\n]%*c");
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • For @OP: For first choice read ["How to `Flush` the input buffer?"](http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392) – Grijesh Chauhan Aug 21 '13 at 05:28
  • I think OP has already tried this _"//An attempt to clear buffer"_ And he wants to avoid twice enter press, when entered input is not overflowing – P0W Aug 21 '13 at 05:35
  • This code should just be move to *after* the call to `fgets`, not before it, and should only be called if the buffer filled by `fgets` did not end in `'\n'`. – R.. GitHub STOP HELPING ICE Aug 21 '13 at 16:28