在 C 文件中读取二进制文件的第一个字符时出现问题
Problem with the first character of the binary file while reading it in a C file
我正在尝试读取二进制文件及其内容。
/*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;
}
但是我在打印这个文件的内容时遇到了问题,因为只有输入的第一个字符没有打印出来,如下所示:
我可以知道为什么会这样吗?
编辑:所有正在读取的变量都声明为 STRINGS
您将 ch
声明为 char
,但您应该将其声明为 int
。
OP 指出文件内容是 'binary' 而不是 'text' 因此,访问文件应该通过为二进制文件制作的 I/O 运算符,
建议:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
由于从 'binary' 文件中读取的数据不是 ascii 字符,因此 'error' 尝试使用 'output format conversion' 说明符打印那些 'binary' 字符: %c
.
建议:
printf( "%02x\n", ch );
注意:%02x
因此前导半字节 0x0 将被打印而不是被抑制。
当代码更正为使用:fread()
而不是 fgetc()
ch
can/should 的声明为 unsigned char ch;
,因此无需更改至 int ch;
建议代码如下:
- 干净地编译
- 执行所需的功能
- 缺少
main()
函数和参数传递:f_name
所以 link
- 打开输入文件时正确检查错误
- 使用从
fread()
到 'assume' EOF 的返回值,但是,检查 errno
的值只是为了确保存在没有其他错误。
- 记录包含每个头文件的原因
注意:建议的代码不是很有效,因为它一次只读取一个字节,而不是整个缓冲区的字节数
注意:建议的代码将在一行中输出一个字节的内容(十六进制)。您可能想修改它以在移动到新行之前输出几个字节的内容(十六进制)。
现在,建议的代码:
#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);
}
我正在尝试读取二进制文件及其内容。
/*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;
}
但是我在打印这个文件的内容时遇到了问题,因为只有输入的第一个字符没有打印出来,如下所示:
我可以知道为什么会这样吗?
编辑:所有正在读取的变量都声明为 STRINGS
您将 ch
声明为 char
,但您应该将其声明为 int
。
OP 指出文件内容是 'binary' 而不是 'text' 因此,访问文件应该通过为二进制文件制作的 I/O 运算符,
建议:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
由于从 'binary' 文件中读取的数据不是 ascii 字符,因此 'error' 尝试使用 'output format conversion' 说明符打印那些 'binary' 字符: %c
.
建议:
printf( "%02x\n", ch );
注意:%02x
因此前导半字节 0x0 将被打印而不是被抑制。
当代码更正为使用:fread()
而不是 fgetc()
ch
can/should 的声明为 unsigned char ch;
,因此无需更改至 int ch;
建议代码如下:
- 干净地编译
- 执行所需的功能
- 缺少
main()
函数和参数传递:f_name
所以 link - 打开输入文件时正确检查错误
- 使用从
fread()
到 'assume' EOF 的返回值,但是,检查errno
的值只是为了确保存在没有其他错误。 - 记录包含每个头文件的原因
注意:建议的代码不是很有效,因为它一次只读取一个字节,而不是整个缓冲区的字节数
注意:建议的代码将在一行中输出一个字节的内容(十六进制)。您可能想修改它以在移动到新行之前输出几个字节的内容(十六进制)。
现在,建议的代码:
#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);
}