gpt4 book ai didi

c - fread 为什么以二进制模式读取以 ff fe 开头的文件返回 1

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:37 25 4
gpt4 key购买 nike

我正在尝试读取 UTF 文件并决定以二进制模式读取它并跳过非 ASCII,因为文件基本上由有效的英文文本组成。我被困在 fread 返回 1 而不是请求的字节数。 print_hex 恕我直言的第一个输出显示它读取了超过 1 个字符。我已经阅读了一些在 C 中读取二进制文件的示例,例如 Read and write to binary files in C? , 阅读关于 fread 例如这里https://en.cppreference.com/w/c/io/fread在这里 How does fread really work? ,还是不解为什么会返回1。文件hexdump,并在下面完成C代码和输出。

ADD:由gcc编译,在Linux上运行。

文件:

00000000  ff fe 41 00 41 00 42 00  61 00 0d 00 0a 00 41 00  |..A.A.B.a.....A.|
00000010 41 00 45 00 72 00 0d 00 0a 00 66 00 73 00 61 00 |A.E.r.....f.s.a.|
00000020 6a 00 0d 00 0a 00 64 00 73 00 61 00 66 00 64 00 |j.....d.s.a.f.d.|
00000030 73 00 61 00 66 00 64 00 73 00 61 00 0d 00 0a 00 |s.a.f.d.s.a.....|
00000040 64 00 66 00 73 00 61 00 0d 00 0a 00 66 00 64 00 |d.f.s.a.....f.d.|
00000050 73 00 61 00 66 00 73 00 64 00 61 00 66 00 0d 00 |s.a.f.s.d.a.f...|
00000060 0a 00 0d 00 0a 00 0d 00 0a 00 |..........|

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_hex(const char *s)
{
while(*s)
printf("%02x ", (unsigned char) *s++);
printf("\n");
}

int main(){

#define files_qty 5
const char* files_array[][2]={{"xx","a"},{"zz","b"},{"xxx","d"},{"d","sd"},{"as","sd"}};

const char* file_postfix = ".txt";

char* file_out_name = "XXX_AD.txt";
FILE* file_out = fopen (file_out_name, "w");

printf ("This app reads txt files with hardcoded names and writes to file %s\n",file_out_name);

/* ssize_t bytes_read = 1; //signed size_t */
size_t n_bytes = 10;
unsigned char* string_in;
char* string_out;
char* file_name;
string_in = (char*) malloc (n_bytes+1);
string_out = (char*) malloc (n_bytes+50);
file_name = (char*) malloc (n_bytes+1); /* more error prone would be to loop through array for max file name length */
int i;
size_t n;

for (i=0;i<files_qty;i++)
{
strcpy (file_name,files_array[i][0]);
FILE* file = fopen (strcat(file_name,file_postfix), "rb");
if (file!= NULL)
{
int k=0;
while (n=fread (string_in, sizeof(char), n_bytes, file)>0)
{
printf("bytes read:%lu\n",(unsigned long) n);
print_hex(string_in);
int j;
for (j=0;j<n;j++)
{
switch (string_in[j])
{
case 0x00:
case 0xff:
case 0xfe:
case 0x0a:
break;
case 0x0d:
string_out[k]=0x00;
fprintf (file_out, "%s;%s;%s\n", files_array[i][0], files_array[i][1], string_out);
k=0;
printf("out:\n");
print_hex(string_out);
break;
default:
string_out[k++]=string_in[j];
}

}
}
fclose (file);
}
else
{
perror (file_name); /* why didn't the file open? */
}
}
free (string_in);
free (string_out);
free (file_name);
return 0;
}

输出:

bytes read:1
ff fe 41
bytes read:1
0d
out:

bytes read:1
72
bytes read:1
61
bytes read:1
73
bytes read:1
61
bytes read:1
0d
out:
72 61 73 61
bytes read:1
61
bytes read:1
73
bytes read:1
61
bytes read:1
0a

最佳答案

你有一个优先问题。简单赋值的优先级低于比较。所以下面一行:

while(n=fread (string_in, sizeof(char), n_bytes, file)>0)

被评估为(额外的括号)

while (n=(fread (string_in, sizeof(char), n_bytes, file)>0))

因此 n 被分配为 1 因为 fread 返回值 > 0

相反,显式添加括号:

while((n=fread (string_in, sizeof(char), n_bytes, file))>0)

关于c - fread 为什么以二进制模式读取以 ff fe 开头的文件返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56134354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com