在 C 中从 stdin 读取内容后,如何再次从 stdin 读取一行?
how to read a line from stdin again after reading something from stdin in C?
这个程序运行没问题。
int main()
{
{
printf("Type something:\n");
char* message = malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message ist : %s\n", message);
free(message);
}
}
但是当我运行下面的程序时,它不让我写任何东西,它打印"message ist: "
int main()
{
char action;
while(action!='e')
{
printf("print a line: p\n");
printf("End Program: e\n");
action = getc(stdin);
if(action == 'p')
{
fflush(stdin);
printf("Type something:\n");
char* message = malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message ist : %s\n", message);
free(message);
}
else if(action == 'e')
{
printf(" Program ended successfully\n");
exit(0);
}
}
}
有没有人解释一下为什么让我在第一个程序中输入,
为什么它不让我在第二个程序中输入?
我尝试刷新键盘输入,但没有用。
我尝试使用 getline()
而不是 fgets()
,结果相同。
如果有任何想法和解释,我将不胜感激。
似乎 fflush(stdin)
(如前所述未定义)无法正常工作。问题是 '\n'
仍在缓冲区中,必须将其删除。否则调用 fgets
,在缓冲区中找到 '\n'
(标记输入结束)并继续执行程序。
试试这个:
// fflush(stdin);
while (getchar() != '\n');
printf("Type something:\n");
char* message = (char*) malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message is : %s\n", message);
free(message);
同样有效(但可能是无意的)的是像 "p MyMessage" 这样的输入。这确实打印了消息。
#include <stdio.h>
void customFlush()
{
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main()
{
char action;
char message[64] = { };
while(action != 'e')
{
printf("---------\nCommands:\n'p' for print a line\n'e' for end program\n\nType a command: ");
action = getc(stdin);
// Exclude unnecessary chars (<Enter> and so on)
customFlush(); // or fseek(stdin, 0, SEEK_END);
if (action == 'p')
{
memset(message, 0, sizeof(message));
printf("\nType something:\t");
fgets(message, 64, stdin);
printf("\nTyped message:\t%s\n", message);
// Here is also possible place for calling customFlush or fseek()
}
}
printf("Program ended successfully\n");
}
这个程序运行没问题。
int main()
{
{
printf("Type something:\n");
char* message = malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message ist : %s\n", message);
free(message);
}
}
但是当我运行下面的程序时,它不让我写任何东西,它打印"message ist: "
int main()
{
char action;
while(action!='e')
{
printf("print a line: p\n");
printf("End Program: e\n");
action = getc(stdin);
if(action == 'p')
{
fflush(stdin);
printf("Type something:\n");
char* message = malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message ist : %s\n", message);
free(message);
}
else if(action == 'e')
{
printf(" Program ended successfully\n");
exit(0);
}
}
}
有没有人解释一下为什么让我在第一个程序中输入, 为什么它不让我在第二个程序中输入?
我尝试刷新键盘输入,但没有用。
我尝试使用 getline()
而不是 fgets()
,结果相同。
如果有任何想法和解释,我将不胜感激。
似乎 fflush(stdin)
(如前所述未定义)无法正常工作。问题是 '\n'
仍在缓冲区中,必须将其删除。否则调用 fgets
,在缓冲区中找到 '\n'
(标记输入结束)并继续执行程序。
试试这个:
// fflush(stdin);
while (getchar() != '\n');
printf("Type something:\n");
char* message = (char*) malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message is : %s\n", message);
free(message);
同样有效(但可能是无意的)的是像 "p MyMessage" 这样的输入。这确实打印了消息。
#include <stdio.h>
void customFlush()
{
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main()
{
char action;
char message[64] = { };
while(action != 'e')
{
printf("---------\nCommands:\n'p' for print a line\n'e' for end program\n\nType a command: ");
action = getc(stdin);
// Exclude unnecessary chars (<Enter> and so on)
customFlush(); // or fseek(stdin, 0, SEEK_END);
if (action == 'p')
{
memset(message, 0, sizeof(message));
printf("\nType something:\t");
fgets(message, 64, stdin);
printf("\nTyped message:\t%s\n", message);
// Here is also possible place for calling customFlush or fseek()
}
}
printf("Program ended successfully\n");
}