Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c - Why does scanf fail but fgets works?

I am asking the user for input on whether they would like to quit the program or not. There are two snippets. One reads input using the scanf() function, while the second reads input using the fgets() function. Using scanf(), the program goes in an infinite loop. Using fgets(), the program performs as intended. Why does scanf() fail, and fgets() work? How can i correct it so that scanf() will work? Here is the code:

First is with scanf()

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

int main(void)
{

    char yesNo[6];

    printf("Enter [quit] to exit the program, or any key to continue");
    scanf("%s", &yesNo[6]);

    while (strcmp(yesNo,"quit
") != 0)
    {
         printf("Enter [quit] to exit the program, or any to continue");
         scanf("%s", &yesNo[6]);
    }

    return 0;
}

Second is with fgets()

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

int main(void)
{

    char yesNo[6];

    printf("Enter[quit] to exit the program, or any key to continue: ");
    fgets(yesNo, 6, stdin);

    while (strcmp(yesNo,"quit
") != 0)
    {
         printf("Enter [quit] to exit the program, or any key to continue:");
         fgets(yesNo, 6, stdin);
    }

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The difference between scanf("%s") and fgets you've to keep in mind is the way they take in input.

%s instructs scanf to discard all leading whitespace characters and read in all non-whitespace characters until a whitespace character (or EOF). It stores all the non-whitespace characters in its corresponding argument, in this case, yesNo, and then leaves back the last whitespace character back into the standard input stream (stdin). It also NUL-terminates its corresponding argument, in this case yesNo.

fgets reads in all input until a newline character (' ') or until the maximum number of characters to read passed in as the second argument minus one (for the NUL-terminator '') has been read (or until EOF) and all this input, including the , is stored in its first argument, yesNo here, and it is NUL-terminated.

So, if you have scanf("%s", yesNo); with an input of quit , yesNo will contain just quit and the will be left in the stdin. Since the strings "quit" and "quit " aren't the same, strcmp will not return zero and the while loop will continue to loop.

For fgets(yesNo, 6, stdin); with the input quit , yesNo will hold quit and the stdin will be empty. strcmp returns zero as both the strings "quit " and "quit " are equal, and execution comes out of the loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...