简体   繁体   English

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

[英]Problem with the first character of the binary file while reading it in a C file

I am trying to read a binary file and its content.我正在尝试读取二进制文件及其内容。

/*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;
}

But I am facing issues when I print the content of thi file because only the first character of the input isn't printed ok, as follows:但是当我打印这个文件的内容时我遇到了问题,因为只有输入的第一个字符没有打印好,如下所示:

在此处输入图像描述

May I know why this is happening?我可以知道为什么会这样吗?

EDIT: All the variables which are being read are declared as STRINGS编辑:正在读取的所有变量都声明为字符串

The OP states the file contents are 'binary' not 'text' Therefore, accessing the file should be via the I/O operators made for binary files, OP 声明文件内容是“二进制”而不是“文本”因此,访问文件应该通过为二进制文件制作的 I/O 操作符,

Suggest:建议:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

Since the data read from the 'binary' file is NOT ascii characters, it is an 'error' to try to print those 'binary' characters with the 'output format conversion' specifier: %c .由于从“二进制”文件读取的数据不是 ascii 字符,因此尝试使用“输出格式转换”说明符打印那些“二进制”字符是一个“错误”: %c

Suggest:建议:

printf( "%02x\n", ch );

Note: the %02x so a leading nibble of 0x0 will be printed rather than suppressed.注意: %02x因此将打印而不是抑制 0x0 的前导半字节。

When the code is corrected to use: fread() rather than fgetc() the declaration of ch can/should be unsigned char ch;当代码被更正以使用: fread()而不是fgetc()时, ch的声明可以/应该是unsigned char ch; so no need to change that to int ch;因此无需将其更改为int ch;

The following proposed code:以下建议的代码:

  1. cleanly compiles干净地编译
  2. performs the desired functionality执行所需的功能
  3. is missing a main() function and the passing of the parameter: f_name so does not link缺少main() function 和参数的传递: f_name所以不链接
  4. properly checks for an error when opening the input file打开输入文件时正确检查错误
  5. uses the returned value from fread() to 'assume' EOF, however, it may be instructive (and for robust code) to check the value of errno just to assure there was no other error.使用fread()的返回值来“假设”EOF,但是,检查errno的值以确保没有其他错误可能是有益的(并且对于健壮的代码)。
  6. documents why each header file is included记录为什么包含每个 header 文件

Note: the proposed code is not very efficient as it only reads a single byte at a time rather than a whole buffer full of bytes注意:建议的代码效率不高,因为它一次只读取一个字节,而不是一个充满字节的整个缓冲区

Note: the proposed code will output one byte contents (in hex) on a single line.注意:建议的代码将 output 单行上的一个字节内容(十六进制)。 You might want to modify that to output several bytes contents (in hex) before moving to a new line.在移动到新行之前,您可能需要将其修改为 output 几个字节内容(十六进制)。

and now, the proposed code:现在,建议的代码:

#include <stdio.h>    // FILE, fopen(), perror(), printf(), fclose()
                      // fread()
#include <stdlib.h>   // exit(), EXIT_FAILURE

void myfunc( char *f_name )
{
    unsigned char ch;
    FILE *fp = fopen( f_name, "rb");
    if (fp == NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

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

    size_t bytesRead;
    while ( ( bytesRead = fread( &ch, 1, 1, fp ) ) == 1 )
    {
        printf("%02x\n", ch);
    }

    fclose(fp);
}

You declared ch as char but you should have declared it as int .您将ch声明为char但您应该将其声明为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM