我无法检测到新行后如果新行在前一行有文本,则在文件末尾之前。
我可以检测到新的第 4 行
1 some text
2 some text
3
4
但不是这种类型文件的新第 3 行
1 some text
2 some text
3
(数字(1,2,3,4)是行,它们不在文件中)
有我的示例代码:(编辑:代码更新为更相关的变量)
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int ch;
int i = 0;
int line = 1;
int column = 0;
int tmp = 0;
fp = fopen("input.txt", "r");
if( fp != NULL ){
while((ch = fgetc(fp)) != EOF) {
switch(ch) {
case '\n':
if(i == 0) {
column = tmp;
}
if(tmp != column) {
printf("error\n");
}
tmp = 0;
line++;
i++;
break;
default:
tmp++;
break;
}
}
fclose(fp);
}
}
我不明白为什么在第二种情况下没有输出“错误”。