【发布时间】:2013-12-08 10:31:21
【问题描述】:
我有点困惑为什么输入没有正确地与“历史”进行比较。无论我输入什么,它似乎都不会进入 if 语句。一旦我输入历史,它就会进入 if 语句。我尝试使用 scanf("%s\n", input);也可以看到并且可以正常工作,但不是我想要的方式。
while(fgets(input, sizeof(input), stdin) != NULL){
filePrint = fopen(".simpleshell_history", "a");
fileRead = fopen(".simpleshell_history", "r");
count++;
fprintf(filePrint, "%d - %s", count, input);
fclose(filePrint);
if (strcmp(input,"history")==0){
printf("%s\n", input);
fseek(fileRead, 0, SEEK_SET);
int x = 0;
while ((x = fgetc(fileRead)) != EOF){
printf("%c", x);
}
}
}
【问题讨论】:
-
fgets在缓冲区末尾留下一个换行符。 -
换行符包含在
input -
感谢您的帮助!就是这样。