C 库函数 feof(FILE*) 判断文件末尾的问题
来源:互联网 发布:java获取磁盘io 编辑:程序博客网 时间:2024/05/21 07:09
C 库函数 feof(FILE*) 判断文件末尾的问题A Problem on Using C Library Function feof(FILE*) to Judge The End of A File我用 C 写了一个程序读取 32768 字节大小的文件,每次读 16 个字节,应该是 2048 次读完。但结果读了 2049 次,并且最后两次的数据相同,似乎重复读取了最后 16 个字节。源代码如下: I wrote a program with C, which read a file of 32768 bytes, 16 bytes each time, and it should finish reading after 2048 times. But the reault was it read 2049 times, and the data of last two times are the same, which seemed the last 16 bytes were read twice. Here is the code:我看了一阵,发现导致这个错误的原因是 feof(FILE*) 判断文件末尾的机制:文件指针在文件末尾的时候,除非再读一次导致发生 I/O 错误,feof(FILE*) 依然返回 0。因此用feof(FILE*) 作为判断条件的 while 循环始终会多读一次,而最后一次的读取是失败的,buffer 也就没有改变,看起来就像是重复读了一次。 I reviewed it for a whil and found the reason that produced this error is the mechanism feof(FILE*) used to judge the end of a file: When the file pointer is at the end of a file, feof(FILE*) still returns 0 unless reads one more time to course a I/O error. Therefore, a while loop using feof(FILE*) as the judgment condition always reads one more time, and the last time of reading will fail, so buffer stayed unchanged which looked like it repeated reading once.用下面的代码就没问题了: Use the code below to solve this problem:
- int loop = 0;
- while (!feof(file)) {
- loop++;
- fread(buffer, 16, 1, file);
- ......
- }
- printf("%d\n", loop); // 2049
- int loop = 0;
- while (fread(buffer, 16, 1, file) == 1) {
- loop++;
- ......
- }
- printf("%d\n", loop); // 2048
- C 库函数 feof(FILE*) 判断文件末尾的问题
- C语言feof()函数判断文件位置指针到末尾
- c语言 判断文件结束问题 feof()
- feof 函数在末尾的问题
- 关于EOF和feof()判断文件读到末尾
- 关于C语言feof()函数判断文件结尾的问题分析
- EOF与feof(),文件末尾的判定方法
- 文件检测函数 feof, ferror,clearerr C库函数
- C语言库函数文件操作(fread、fwrite、feof)
- 文件检测函数 feof, ferror,clearerr C库函数
- 文件检测函数 feof, ferror,clearerr C库函数
- feof 作为循环判断条件的问题
- 文件末尾检测函数feof()
- C中判断文件结束的两种方法feof()和EOF
- 用feof()判断文件时出现多读内容的问题
- 用feof()判断文件时出现多读内容的问题
- 文件结尾判断feof , EOF
- 判断文件结束的两种方法: EOF和feof()
- http设置无缓存
- hadoop in action第一个例子编译错误处理
- POJ 1050 To the Max
- POI导出EXCEL经典实现(附带jar包)
- mkimage的-a 和 –c参数和内核引导
- C 库函数 feof(FILE*) 判断文件末尾的问题
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- C#访问修饰符
- MyGeneration “Drivers” dropdown list Empty 无数据库驱动
- nutch1.0 在windows环境下的安装与使用
- 在VC中调用 WebService (非托管)
- C#调用执行程序
- Java正则表达式详解
- 监听EditText的变化--TextWatcher