C语言 接受来自 scanf 函数的任意数量的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15291523/
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
Accepting any number of inputs from scanf function
提问by user2147954
I am trying to read an unknown number of inputs using scanffunction.
我正在尝试使用scanf函数读取未知数量的输入。
int a[100];
int i = 0;
while((scanf("%d", &a[i])) != '\n')
i++;
// Next part of the code
But this function is not going to next part of the code, seems like there is an infinite while loop.
但是这个函数不会进入下一部分代码,似乎有一个无限的while循环。
How Do I solve this logical error? Is there any other alternatives to scanflike sscanfto read integers into an array?
我如何解决这个逻辑错误?是否有任何其他替代scanf喜欢sscanf读整数到一个数组?
回答by LihO
scanfreturns the number of input items that have been successfully matched and assigned, thus it is reasonable to do:
scanf返回已成功匹配并分配的输入项的数量,因此这样做是合理的:
while(scanf(...) == 1)
Now you want to be able to read multiple numbers, each defined on the new line. Then you could simply do this:
现在您希望能够读取多个数字,每个数字都定义在新行上。然后你可以简单地这样做:
int array[100];
int i = 0;
while(i < 100 && scanf("%d\n", &array[i]) == 1)
i++;
note that this reading will stop only if invalid input is entered (for example letter q) or when you input the end-of-input control code, which is Ctrl+Z(on Windows) or Ctrl+D(on Mac, Linux, Unix).
请注意,只有在输入无效输入(例如字母q)或输入结束输入控制代码时才会停止读取,即Ctrl+ Z(在 Windows 上)或Ctrl+ D(在 Mac、Linux、Unix 上)。
回答by Klas Lindb?ck
The return value of scanf is the number of input items successfully matchedand assigned, so try this to end when a non-numeric input is encountered:
scanf 的返回值是成功匹配并分配的输入项的数量,因此在遇到非数字输入时尝试这样结束:
while (i < 100 && (scanf("%d", &a[i])) == 1) { i++; }
回答by MOHAMED
First replace "%d"by " %d"this will avoid the new line problems
首先替换"%d"为" %d"this 将避免换行问题
second if your input contain non numeric input then your while loop will enter in the infinite loop. so you have to check your input if it's a numeric input
其次,如果您的输入包含非数字输入,那么您的 while 循环将进入无限循环。所以你必须检查你的输入是否是数字输入
回答by Avidan Borisov
You want to do
你想做
char discard;
while(i < 100 && (scanf("%d%1[^\n]s", &arr[i], &discard)) == 2)
i++;
to keep getting input till a new line.
保持输入直到新的一行。
scanf() isn't the best tool for reading entire lines. You should use fgets() to read the line and then parse it using sscanf() or other functions.
scanf() 不是阅读整行的最佳工具。您应该使用 fgets() 读取该行,然后使用 sscanf() 或其他函数对其进行解析。
回答by Aki Suihkonen
The return value of scanf is the number of scanned inputs per call. You can compare it to integer 10 (or '\n'), which would stop the loop when the scanf actually read 10 items. (And to do that, it would have to have ten specifiers in the format string.
scanf 的返回值是每次调用的扫描输入数。您可以将其与整数 10(或 '\n')进行比较,这将在 scanf 实际读取 10 个项目时停止循环。(为此,格式字符串中必须有十个说明符。
You can try
你可以试试
while((i<10) && (scanf("%d",a+i)==1)) i++;
Accepting any number of arguments has to be programmed eg. as
必须对接受任意数量的参数进行编程,例如。作为
while (fgets(mybuf, ALLOCATED_LENGTH-1, stdin)) {
char *p = mybuf;
if (*p=='\n') break; // or a better function to test for empty line
while (custom_scan_next_integer(&p)) i++;
}
where custom_scan_next_integermodifies the pointer p to forward the proper amount of characters.
wherecustom_scan_next_integer修改指针 p 以转发适当数量的字符。

