为什么以二进制模式读取以fffe开头的文件返回1

时间:2019-05-14 15:54:50

标签: c fread

我试图读取UTF文件,并决定以二进制模式读取它,并跳过非ASCII,因为文件基本上由有效的英文文本组成。我被fread困住了,返回的不是1而是请求的字节数。恕我直言,print_hex的第一个输出显示它已读取多个字符。我已经阅读了一些示例,这些示例在C中读取二进制文件,例如Read/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

1 个答案:

答案 0 :(得分:5)

您有优先权问题。简单分配的优先级比比较低。所以下面的行:

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)