Ç - 相当于.NET到Console.ReadLine的
题
我需要完成的相同的行为.NET到Console.ReadLine功能提供。当用户按下输入键的程序执行应继续进行。
下面的代码是不充分的,因为它需要额外的输入:
printf ("Press Enter to continue");
scanf ("%s",str);
任何建议?
解决方案
您可以用fgets(),像这样:
#include <stdio.h>
fgets(buf, sizeof(buf), stdin);
其他提示
使用函数getchar()
尝试这样:
printf ("Press Enter to continue");
scanf(“%[^\n]“,str);
getline
可能比在大多数情况下getchar
更好。它可以让你的“进入”,并且不受缓冲区溢出之前捕捉到所有用户的输入。
char *buf=NULL;
printf("Press enter to continue: ");
getline(&buf,0,stdin);
// Use the input if you want to
free(buf); // Throw away the input
您可以使用与scanf函数做一段时间。
do{
scanf("%s",str1);
}while(str1[0] == '\0' || str1[0] == '\r' || str1[0] == '\n');
不隶属于 StackOverflow