我不明白为什么我的字符串比较结果不正确。这是关于C语言的。
它从一个文件中读取数据,文件的格式如下:
1 - ls
2 - cd
3 - history
如果我输入!c,它应该获取最后一个以“c”开头的使用过的字符串并运行命令。但它从未执行if(strcmp())=0)语句。
这是我的代码的一部分:
char currLine[MAXINPUTLINE];
else if (isalpha(input[1])){
int count = 1;
fileRead = fopen(".simpleshell_history", "r");
while(fscanf(fileRead, "%s\n", currLine) != EOF){
printf(input+1);
printf(currLine);
if(strcmp((input+1), currLine) == 0){
printf("%s\n", currLine);
parse(currLine);
}
}
}
这是while循环中printf语句打印的内容,我无法弄清如何修复它,并且已经陷入困境一段时间。这是当我输入'!c'时发生的。
c
1c
-c
lsc
2c
-c
cdc
3c
-c
historyc
4c
-c
!c!c
\n
。请展示一下你是如何获取“input”的。 - chux - Reinstate Monica