fgets 之后获取键盘输入
我一生都无法平静地对待 C 字符串和输入/输出。
对于我的程序,我只需输入一个字符串,然后在以下代码中对其进行处理:(tmpstring 和 ch 已定义)
对于我的传入输入,我在终端中写入: echo "test" | ./program
int main(int argc, char *argv[]) {
char tmpstring[2048];
int ch;
int r;
int c;
fgets(tmpstring, sizeof tmpstring, stdin);
while((ch = fgetc(stdin))!= EOF && ch != '\n');
tmpstring[strlen(tmpstring)-1]='\0';
strncpy(opponent, tmpstring+1, strlen(tmpstring+1));
move();
move() 内部;
char buffer[2048]={0};
int r, c;
r=0; c=0;
printf("Your move (row column):");
if((fgets(buffer, sizeof buffer, stdin)==NULL) || ((sscanf(buffer,"%d %d", &r, &c))!=2)){
printf("Invalid input. Please insert two numbers separated by whitespace.\n");
exit(1);
}
//continues
执行此操作会直接进入无效输入,而不要求更多输入。我已经阅读了有关如何不应该清除标准输入的所有内容(并且这是不可能的),但我真的不知道该怎么办。我显然试图用 while 循环的第二部分“转储”stdin。我已经改变了 &&在第一个条件之后到||在 while 循环中。没有变化。总的来说,在使用了 fgets 后如何请求更多输入?
*编辑:更多代码并分离原始 while 循环
I, for the life of me, cannot be at peace with c strings and input/output.
For my program I simply enter a string and it gets processed in the following code: (tmpstring and ch are already defined)
For my incomning input, I write in terminal: echo "test" | ./program
int main(int argc, char *argv[]) {
char tmpstring[2048];
int ch;
int r;
int c;
fgets(tmpstring, sizeof tmpstring, stdin);
while((ch = fgetc(stdin))!= EOF && ch != '\n');
tmpstring[strlen(tmpstring)-1]='\0';
strncpy(opponent, tmpstring+1, strlen(tmpstring+1));
move();
Inside of move();
char buffer[2048]={0};
int r, c;
r=0; c=0;
printf("Your move (row column):");
if((fgets(buffer, sizeof buffer, stdin)==NULL) || ((sscanf(buffer,"%d %d", &r, &c))!=2)){
printf("Invalid input. Please insert two numbers separated by whitespace.\n");
exit(1);
}
//continues
Executing this goes straight into the invalid input without asking for more input. I've read all around about how you shouldn't clear stdin (and that it's impossible) but I really don't know what to do. I clearly tried to "dump" stdin with the second part of the while loop. I've changed the && after the first condition to an || in the while loop. No change. Overall, how can I ask for more input after already used fgets?
*edit: more code and separated the original while loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此类问题已在其他地方讨论过。
我将从这里复制我自己帖子的一部分:
Max使用 scanf 的字符串长度 -> ANSI C
stdarg.h
库定义了一个具有可变数量参数的函数my_scanf()
,并结合fgets() 和 vsscanf()。
fgets()
和sscanf()
的组合(实际上,我们必须使用vsscanf()
为了正确处理参数列表)。my_scanf()
代替。这里有代码:
函数 my_scanf() 具有原型
fmt
,其行为方式与任何其他scanf()
类似的行为相同。返回值是一个int,如果
maxbuff
没有意义,或者发生了一些输入错误,则返回EOF
。如果返回非负值,则与标准函数sscanf()
或vsscanf()
返回的值相同。maxbuff
以 1 递增,因为fgets()
为额外的“\0”字符腾出了一些空间。maxbuff
的非正值将被立即丢弃。fgets()
将从stdin
(键盘)读取一个字符串,最多可容纳maxbuff
个字符,包括“\n”。stdin
后fgets()
尚未到达'\n'。buffer[maxbuff - 1]
不等于“\0”或“\n”时,此值为真。 (检查一下!)stdarg.h
宏 和函数vsscanf()
的适当(和标准)组合来处理变量参数列表。This kind of problem has been discussed elsewhere.
I will copy a part of my own post, from here:
Max string length using scanf -> ANSI C
my_scanf()
with variable number of parameters, by invoking thestdarg.h
library, joint to a combination offgets()
andvsscanf()
.fgets()
andsscanf()
(actually, we have to usevsscanf()
in order to properly process the list of arguments).scanf()
would return. So, you can usemy_scanf()
instead.Here you have the code:
The function my_scanf() has the prototype
fmt
that behaves in the same way as any otherscanf()
-like does.The return value is an int, which is
EOF
ifmaxbuff
has no sense, or well some input error happened. If a non-negative value is returned, it is the same that would be returned by the standard functionssscanf()
orvsscanf()
.maxbuff
is incremented in 1, becausefgets()
makes some room for an additional '\0' character.maxbuff
are immediatelly discarded.fgets()
will read a string fromstdin
(keyboard) with room for at mostmaxbuff
characters, including '\n'.fgets()
has not reached '\n' after readingstdin
.buffer[maxbuff - 1]
is not equal to '\0' nor '\n'. (Check it!)stdarg.h
macros and the functionvsscanf()
is employed to process the variable list of parameters.