C编程ftell fseek fread,读取文件大小

发布于 2024-08-13 05:35:44 字数 887 浏览 9 评论 0原文

我有一个文件。我读取了文件的大小。然后我循环一次读取两个字节,直到到达文件末尾。每次读取操作后,我都会将当前位置增加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-08-20 05:35:44
readCount = fread(&val, sizeof(val), 2, file);  

该语句读取两个项目,每个项目两个字节。它返回值2,表示读取的项目数。第二个和第三个参数相乘告诉 fread 要读取多少字节。

readCount = fread(&val, sizeof(val), 2, file);  

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.

梦幻的心爱 2024-08-20 05:35:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文