log.txt 文件包含一些数据。该程序寻找“:”,当它找到时打印“完成”。该程序编译成功但从未打印“完成”。
char *atrbt ;
FILE *fp;
int i = 0;
if (fp = fopen("log.txt", "r+")) {
while (fscanf(fp, "%c", &atrbt) != EOF) {
printf("%c", atrbt);
if(atrbt[i] == ':') { <------------ Issue
printf("Done");
}
++i;
}
}
最佳答案
您正在混合使用 char 和 char 指针。一种可能的正确方法是(代码未经测试):
char atrbt;
FILE *fp;
if (fp = fopen("log.txt", "r+")) {
while ((atrbt = getc(fp)) != EOF) {
printf("%c", atrbt);
if(atrbt == ':') {
printf("Done");
}
}
}
关于c - 字符串操作中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44853110/