无法使用C中的fread读取文件(Unable to read file using fread in C)

系统教程 行业动态 更新时间:2024-06-14 17:01:31
无法使用C中的fread读取文件(Unable to read file using fread in C)

我试图一次读取文件“file.raw”和4个字节到一个数组,并检查它是否具有我正在寻找的特定4字节签名。 我遇到了麻烦。 我得到的结果值是0,而不是使用fread时的4。

#include<stdint.h> #include<stdio.h> #include<stdlib.h> #include<string.h> typedef uint8_t BYTE; int main(void) { size_t result; FILE *inptr = fopen("file.raw","r+"); //Check if file can be opened. if (inptr == NULL) { printf("File Open Error\n"); return -1; } long int x = 0; while(!feof(inptr)) { // Make space for reading in to an array BYTE *array = (BYTE *) malloc(10); if(array == NULL) { printf("Array Initialization Error\n"); return -1; } result = fread(array,1,4,inptr); //Exit if file not read. ** This is where I can't get past. if(result != 4) { printf("File Read Error\n"); printf("%d\n",result); free(array); fclose(inptr); return -1; } //Compare strings if(memcmp(array,"0xffd8ffe0",4)==0) { printf("File Start found\n"); printf("Exiting...\n"); printf("%p\n",inptr); free(array); fclose(inptr); return 0; } x++; free(array); } printf("%p\n",inptr); printf("%ld\n",x); fclose(inptr); return 0; }

I am trying to read a file "file.raw" and 4 bytes at a time to an array and check if it has the particular 4 byte signature I am looking for. I am having trouble with this. The value of result I get is 0, instead of 4 when using fread.

#include<stdint.h> #include<stdio.h> #include<stdlib.h> #include<string.h> typedef uint8_t BYTE; int main(void) { size_t result; FILE *inptr = fopen("file.raw","r+"); //Check if file can be opened. if (inptr == NULL) { printf("File Open Error\n"); return -1; } long int x = 0; while(!feof(inptr)) { // Make space for reading in to an array BYTE *array = (BYTE *) malloc(10); if(array == NULL) { printf("Array Initialization Error\n"); return -1; } result = fread(array,1,4,inptr); //Exit if file not read. ** This is where I can't get past. if(result != 4) { printf("File Read Error\n"); printf("%d\n",result); free(array); fclose(inptr); return -1; } //Compare strings if(memcmp(array,"0xffd8ffe0",4)==0) { printf("File Start found\n"); printf("Exiting...\n"); printf("%p\n",inptr); free(array); fclose(inptr); return 0; } x++; free(array); } printf("%p\n",inptr); printf("%ld\n",x); fclose(inptr); return 0; }

最满意答案

我的猜测是它在while循环的第一次迭代中没有失败,而是继续读取文件,直到你到达文件的末尾,此时fread()返回0并退出你的程序。

它没有找到签名的原因是:

memcmp(array,"0xffd8ffe0",4)==0

那个memcmp()调用几乎肯定不是你想要的(它正在寻找ASCII字符'0' , 'x' , 'f'和'f' )。

PS正如评论中@Mat所述,为了获得最大的可移植性,您应该以二进制模式打开文件( "r+b"而不是"r+" )。

My guess is that it doesn't fail on the first iteration of the while loop, but rather keeps reading the file until you reach end of the file, at which point fread() returns 0 and your program exits.

The reason it's not finding the signature is this:

memcmp(array,"0xffd8ffe0",4)==0

That memcmp() call is almost certainly not what you want (it's looking for the sequence of ASCII characters '0', 'x', 'f' and 'f').

PS As noted by @Mat in the comments, for maximum portability you should open the file in binary mode ("r+b" instead of "r+").

更多推荐

本文发布于:2023-04-20 16:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/39d373fa975e4072d952abd4c2961989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   fread   Unable   file   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!