C编程ftell fseek fread,读取文件大小
我有一个文件。我读取了文件的大小。然后我循环一次读取两个字节,直到到达文件末尾。每次读取操作后,我都会将当前位置增加 2,但是在达到文件大小的一半后,位置不会增加,fread 操作将读取 0 字节。
程序读取文件大小。我执行 fread (每次 2 个字节),直到当前位置等于文件的大小。 它读取文件大小 22915 个字节 每次读取后,它都会将位置增加 2,但是当当前位置达到 11459(文件大小的一半)时,它将读取零字节,从而进入无限循环。
FILE *file;
char *file_name;
int readCount = 0;
int position = 0;
int fileSize;
unsigned short mem_accesses;
file_name = "sample.txt";
/** open the file */
file = fopen(file_name, "rb");
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);
while(position<fileSize){
mem_accesses = getNumberAccesses();
printf("position: %d filesize: %d\n",position, fileSize);
}
unsigned short getNumberAccesses(){
/** calculate number of accesses for process */
unsigned short val;
readCount = fread(&val, sizeof(val), 2, file);
position += readCount;
printf("read count: %d\n", readCount);
return val;
}
I have a file. I read the size of file. Then I loop reading two bytes at a time until I get to the end of the file. After every read operation I increment the current position by 2, however the position does not get incremented after I get to half the size of the file, the fread operation will read 0 bytes.
the program reads the file size. I perform fread (2 bytes everytime) until the current position is equal to the size of the file.
It reads 22915 byes for the file size
It increments position by 2 after every read, however when current position gets to 11459 which is half the size of the file it will read zero bytes thus going into an infinite loop.
FILE *file;
char *file_name;
int readCount = 0;
int position = 0;
int fileSize;
unsigned short mem_accesses;
file_name = "sample.txt";
/** open the file */
file = fopen(file_name, "rb");
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);
while(position<fileSize){
mem_accesses = getNumberAccesses();
printf("position: %d filesize: %d\n",position, fileSize);
}
unsigned short getNumberAccesses(){
/** calculate number of accesses for process */
unsigned short val;
readCount = fread(&val, sizeof(val), 2, file);
position += readCount;
printf("read count: %d\n", readCount);
return val;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该语句读取两个项目,每个项目两个字节。它返回值2,表示读取的项目数。第二个和第三个参数相乘告诉
fread
要读取多少字节。This statement reads two items of two bytes each. And it returns the value 2, for the number of items read. The second and third parameters multiplied together tell
fread
how many bytes to read.fread 返回读取的元素数,而不是字节数。
(顺便说一句,在您的代码中,您错误地将元素计数设置为 2,这会导致缓冲区溢出)。
您应该检查 fread 的返回值,这样在出现错误时就不会陷入无限循环。
我不会查询文件大小,而是简单地循环调用 fread (或其他输入函数),直到文件末尾。
fread return the number of elements read, not number of bytes.
(Incidentally, in your code you mistakenly give it an element count of 2 which causes a buffer overflow).
You should check fread's return value so you won't run into infinite loops in the case of errors.
Instead of querying for the file size, I would simply call fread (or other input functions) in a loop until the end of file.