【发布时间】:2019-10-22 05:17:57
【问题描述】:
假设我有一个包含以下内容的文件:
This line contains more than 75 characters due to the fact that I have made it that long.
Testingstring1
testingstring2
这是我的代码:
void checkLine( char const fileName[]){
FILE *fp = fopen(fileName,"r");
char line[75];
while (1) {
fgets(line,75,fp);
if (feof(fp)){
break;
} else {
printf("%s\n", line);
}
}
}
如何让它只保存变量line中每行的前75个字符?
上面的代码给出以下输出:
This line contains more than 75 characters due to the fact that I have mad
e it that long.
Testingstring1
testingstring2
预期的输出应该是这样的:
This line contains more than 75 characters due to the fact that I have mad
Teststring1
Teststring2
【问题讨论】:
-
请注意,从技术上讲,不可能只读取每行的前 75 个字符。您仍然需要阅读整行才能找到行终止符。