fread why returns 1 对于以 ff fe 开头的文件以二进制模式读取

fread why returns 1 for file starting with ff fe read in binary mode

我正在尝试读取 UTF 文件并决定以二进制模式读取它并跳过非 ASCII,因为文件基本上由有效的英文文本组成。我卡在 fread 返回 1 而不是请求的字节数。 print_hex 恕我直言的第一个输出显示它读取了超过 1 个字符。我已经阅读了一些用 C 读取二进制文件的示例,例如 Read and write to binary files in C?, read about fread e.g. here https://en.cppreference.com/w/c/io/fread and here How does fread really work?,仍然不明白为什么 returns 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)