fgets function problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ocli5568
    New Member
    • Sep 2009
    • 7

    fgets function problem

    What i've written below is a much simplified version of my program but it has the exact same problem (didn't want to bore people with long code).
    I wrote the printf's appropriate to what the code does or doesn't do.
    it doesn't take the fgets (in the function) in the second while loop but never the first, and i want it to take a string in the first.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    int this_is_functio n()
    {
    char anotherBuffer[255];
    char test2[] = "win";

    printf("This fgets doesn't catch first time.\n>> ");
    fgets(anotherBu ffer, 4, stdin);

    printf("%s\n", anotherBuffer);

    if (strcmp(test2,a notherBuffer) == 0){
    printf("success ");
    return 1;
    }

    return 0;
    }

    int main(void)
    {
    char *buffer;
    char test1[] = "hello";

    buffer = (char*)malloc(2 55);

    printf("If i enter some input here.\n>> ");
    fgets(buffer, 6, stdin);

    printf("%s\n", buffer);

    if (strcmp(buffer, test1) == 0){
    while (this_is_functi on() == 0)
    printf("Error") ;
    }

    free(buffer);
    }

    any ideas? thanks in advance
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by ocli5568
    What i've written below is a much simplified version of my program but it has the exact same problem (didn't want to bore people with long code).
    I wrote the printf's appropriate to what the code does or doesn't do.
    it doesn't take the fgets (in the function) in the second while loop but never the first, and i want it to take a string in the first.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    int this_is_functio n()
    {
    char anotherBuffer[255];
    char test2[] = "win";

    printf("This fgets doesn't catch first time.\n>> ");
    fgets(anotherBu ffer, 4, stdin);

    printf("%s\n", anotherBuffer);

    if (strcmp(test2,a notherBuffer) == 0){
    printf("success ");
    return 1;
    }

    return 0;
    }

    int main(void)
    {
    char *buffer;
    char test1[] = "hello";

    buffer = (char*)malloc(2 55);

    printf("If i enter some input here.\n>> ");
    fgets(buffer, 6, stdin);

    printf("%s\n", buffer);

    if (strcmp(buffer, test1) == 0){
    while (this_is_functi on() == 0)
    printf("Error") ;
    }

    free(buffer);
    }

    any ideas? thanks in advance
    There is probably a '\n' left in input stream,which is read in first loop iteration causing fgets to stop receiving input from the stream.


    So you will need to check to see if the input stream is empty before you use fgets,if it is not you'll need to empty it yourself ,something like:

    Code:
    int this_is_function()
    {
    	char anotherBuffer[255];
    	char test2[] = "win";
    
    	printf("This fgets doesn't catch first time.\n>> ");
        
            while(!feof(stdin))
            {
                getchar();
            }
    
    	fgets(anotherBuffer, 4, stdin);
    
    	printf("%s\n", anotherBuffer);
    
    	if (strcmp(test2,anotherBuffer) == 0){
    		printf("success");
    		return 1;
    	}
    
    	return 0;
    }
    NOTE:Usually there will be only '\n' left in stream so you could change the while loop statement for an if statement.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by ocli5568
      Code:
      fgets(buffer, 6, stdin);
      In main when you type in six characters and press the enter key more than six characters are present in the system's input buffer but at maximum only six of them are read. The others are left there for a next read (scanf). Even worse, Microsoft in all its glorious wisdom had decided that the enter key stuffs two characters in your input buffer: \r and \n. If even one of them is left in the input buffer your reading goes berzerk. You have to anticipate on that.

      kind regards,

      Jos

      Comment

      • ocli5568
        New Member
        • Sep 2009
        • 7

        #4
        thanks so much for all the advice. although savage i tried implementing your version, it seemed to get stuck on a while loop and i don't understand enough about the error functions to go about fixing anything there. but i tried initializing my test function as what it is, adding a '\n' to the end of it and that cleared up the problem (using both of your help, again thankyou).
        would that fix the problem consistently? because it seems like a flimsy quick fix.

        Comment

        Working...