3

我需要完成与 .NET Console.ReadLine 函数提供的相同的行为。当用户按下回车键时,程序应该继续执行。

以下代码是不够的,因为它需要额外的输入:

printf ("Press Enter to continue");
scanf ("%s",str); 

有什么建议么?

4

5 回答 5

6

您可以使用 fgets(),如下所示:

#include <stdio.h>

fgets(buf, sizeof(buf), stdin);
于 2010-03-18T19:42:11.897 回答
2

使用函数 getchar()

于 2010-03-18T19:40:25.960 回答
2

试试这个:

printf ("Press Enter to continue"); 
scanf(“%[^\n]“,str);
于 2010-03-18T19:42:06.317 回答
0

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
于 2010-03-18T19:42:26.123 回答
0

您可以将 do while 与 scanf 功能一起使用。

do{
    scanf("%s",str1);
}while(str1[0] == '\0' || str1[0] == '\r' || str1[0] == '\n');
于 2017-01-09T10:41:26.657 回答