您好,我正在使用这段代码读取 txt 中的 float 。如果 txt 文件的末尾有多余的空白空行,程序会将其读取为 0.00000,这会影响我的计算 Last empty line
(#表示计算结束我添加注释行如果存在更新注释行)
我尝试了“getline”和其他我无法修复的函数
fptr = fopen(fileName,"r+");
if(fptr == NULL){
printf("Error!! Cannot open file: %s \n", fileName );
return 1;
}
else {
printf("File opened successfully\n");
while((c = getc(fptr)) != '#' && c != EOF) {
fscanf(fptr,"%f" ,&arr[i]);
++i;
}
}
最佳答案
检查 fscanf
的返回值——它应该在成功读取数字时返回 1,在空白行返回 0。
关于c - 如何检测最后一个额外的空行并在 C 中忽略它(使用 getc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42632659/