输入输出问题:
我被要求从用户那里获取输入并解析它。问题是我的程序应该能够处理任何长度的输入,并将任何超过 256 个字符的输入行视为无效并相应地打印一条消息。
目前我fgets
用来接收输入行并strtok
稍后解析它,但这是一个很长的输入行的问题。
我该如何解决这个问题?
到目前为止我的代码:
char userInput[1024];
char *token = NULL;
while (!feof(stdin)) {
fflush(stdin);
if (fgets(userInput, 1024, stdin) != NULL) {
token = strtok(userInput, " \t\r\n");
if (token != NULL) {
if (strncmp(userInput, "fiver", 5) == 0) {
printf("5");
}
else if (strncmp(userInput, "four", 4) == 0) { printf("4");}