本文介绍了fread为什么以二进制模式读取的以ff fe开头的文件返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取UTF文件,并决定以二进制模式读取它,并跳过非ASCII,因为文件基本上由有效的英文文本组成.我被困在 fread 中,返回1而不是请求的字节数.恕我直言,print_hex的第一个输出显示它已读取多个字符.我已经阅读了一些用C读取二进制文件的示例,例如读取和写入二进制文件文件在C中?,请阅读有关 fread 的信息,例如此处 https://en.cppreference.com/w/c/io/fread和此处 fread实际如何工作?,仍然困惑为什么返回1.文件hexdump,并在下面完成C代码和输出.

I'm trying to read UTF file and decided to read it in binary mode and skip non-ASCII as file consists of valid english text basically. I'm stuck at fread returning 1 instead of number of bytes requested. First output of print_hex IMHO shows it has read more than 1 char. I've read some examples of reading binary files in C e.g 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?, still puzzled why it returns 1. File hexdump, and complete C code and output below.

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

ADD: compiled by gcc, run on 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

推荐答案

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

You have a precedence problem. Simple assignment has lower precedence than comparison. So the following line:

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

Therefore n is being assigned as 1 because fread is returning a value > 0

相反,将括号显式添加为:

Instead, explicitly add parenthesis as:

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

这篇关于fread为什么以二进制模式读取的以ff fe开头的文件返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:51