C语言 在while循环中使用scanf函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19911923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the scanf function in while loop
提问by Julian Laval
I am attempting to format a space-delimited user input for a programming assignment.
我正在尝试为编程作业格式化以空格分隔的用户输入。
Essentially, the input consists of an arbitrary number of expressions
本质上,输入由任意数量的表达式组成
L integer integer integer integerand C integer integer integer.
L integer integer integer integer和C integer integer integer。
For example: L 1 1 5 7 C 4 5 3.
例如:L 1 1 5 7 C 4 5 3。
So far, I've managed to extract the integers depending on the initial character, and can iterate through the string using the scanf function:
到目前为止,我已经设法根据初始字符提取整数,并且可以使用 scanf 函数遍历字符串:
char a;
while(scanf("%c", &a) == 1){
if(a == 'C'){
int inputX, inputY, inputR;
scanf("%d %d %d", &inputX, &inputY, &inputR);
printf("%d %d %d\n", inputX, inputY, inputR);
}
else if(a == 'L'){
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
printf("%d %d %d %d\n", x1, y1, x2, y2);
}
}
Unfortunately, although this outputs the desired integers, the loop (and user input prompt) doesn't terminate.
不幸的是,虽然这会输出所需的整数,但循环(和用户输入提示)不会终止。
Could someone please enlighten me as to why this is happening?
有人能告诉我为什么会这样吗?
回答by haccks
This is because \nis always there to make scanf("%c", &a) == 1always true.
Change your
这是因为\n总是在那里使scanf("%c", &a) == 1总是真实的。
改变你的
while(scanf("%c", &a) == 1)
to
到
while(scanf(" %c", &a) == 1)
// ^space before format specifier.
A space before %cwill eat up this \nleft behind by scanf(on pressing Enter).
前一个空格%c会吃掉(按)\n留下的这个。scanfEnter
回答by T.V.
The reason is scanf reads directly from the standard input and which blocks and waits for user input after it has processed the line. What you need to do is read the line and process that line in your while loop. I've modified your code below.
原因是 scanf 直接从标准输入读取,并且在处理完该行后会阻塞并等待用户输入。您需要做的是在 while 循环中读取该行并处理该行。我在下面修改了你的代码。
char a;
char line[1024];
fgets(line, 1023, stdin); // leave 1 character for null terminator
while(sscanf(line, "%c", &a) == 1){
if(a == 'C'){
int inputX, inputY, inputR;
sscanf(line, "%d %d %d", &inputX, &inputY, &inputR);
printf("%d %d %d\n", inputX, inputY, inputR);
}
else if(a == 'L'){
int x1, y1, x2, y2;
sscanf(line, "%d %d %d %d", &x1, &y1, &x2, &y2);
printf("%d %d %d %d\n", x1, y1, x2, y2);
}
}
回答by chux - Reinstate Monica
Combining some features of other posts and some additions.
Use fgets()and %ninside sscanf(). Be sure to check results of sscanf().
结合其他帖子的一些功能和一些补充。
使用fgets()和%n里面sscanf()。一定要检查结果sscanf()。
char line[1024];
while (fgets(line, sizeof line, stdin) != NULL)) {
char *s = line;
char Type;
int n;
while(sscanf(s, " %c%n", &Type, &n) == 1) {
s += n;
if(Type == 'C') {
int inputX, inputY, inputR;
if (3 != sscanf(s, "%d %d %d%n", &inputX, &inputY, &inputR, &n)) {
Handle_Syntax_Error();
}
s += n;
printf("%d %d %d\n", inputX, inputY, inputR);
}
else if(Type == 'L') {
int x1, y1, x2, y2;
if (4 != sscanf(s, "%d %d %d %d%n", &x1, &y1, &x2, &y2, &n)) {
Handle_Syntax_Error();
}
s += n;
printf("%d %d %d %d\n", x1, y1, x2, y2);
}
else {
Handle_Syntax_Error();
}
}
}

