在C文件中读取二进制文件的第一个字符时遇到的问题

问题描述 投票:0回答:1

我正在尝试读取二进制文件及其内容。

/*aoObj.fb is the pointer of the file (e.x. FILE *fp)*/

char ch;
aoObj.fp = fopen(aoObj.f_name, "rb");

if (aoObj.fp == NULL)

      {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
      }

        /* /\*list all strings *\/ */
        printf("\n\nThe content of the file: \n");

        while ((ch = fgetc(aoObj.fp)) != EOF)
          printf("%c", ch);

        fclose(aoObj.fp);
        (void) opt_free(&aoObj);
        return 0;
}

但是当我打印此文件的内容时遇到了问题,因为只有输入的第一个字符不能正确打印,如下所示:

enter image description here

我可以知道为什么会这样吗?

编辑:所有正在读取的变量都声明为STRINGS

c linux binaryfiles
1个回答
0
投票

您将ch声明为char,但应将其声明为int

© www.soinside.com 2019 - 2024. All rights reserved.