【问题标题】:reading from a binary file into an int从二进制文件读入 int
【发布时间】:2015-04-20 22:03:57
【问题描述】:

我对二进制文件以及如何读取它们感到有些困惑,所以如果有人可以提供帮助,那就太好了。 我有一个包含以下位的文件:

00000000000000000000000000000111 

(32 位。我数了)

现在我写了这段代码:

int main()
{
    FILE * f1;
    f1 = fopen("file2", "rb");
    int i = 0;
    fread(&i, sizeof(int), 1, f1);
    printf("%d", i);
    fclose(f1);
    return 0;

}

打印出我 808464432。 为什么?它不应该打印 7 吗? 感谢您的阅读。

【问题讨论】:

  • 如何查看文件内容?
  • 打印出您的sizeof(int)。您可能正在处理一个 2byte int,这意味着您正在读取两个字节的 0 位,这意味着您的“int”将是 0。你是怎么数位的?如果您的文件是“二进制”文件,那么它的文件大小为 4 个字节。如果是文本,则为 32 个字节。
  • 我的sizeof(int)是4,不好意思误导了你,输出是808464432,不是0。
  • ...这意味着,Iharob 的猜测是正确的,并且该文件不包含 bits 0000....,但包含 0 个字符(ASCII 中的 0x30),后跟一些 1 个字符。 808464432 = 0x30303030
  • fwrite(&i, sizeof i, 1, f1) ;) 请注意,这些文件是不可移植的(您不能在一台机器上写入并在另一台机器上读取)并且对于使用不同编译器编译的代码可能不一致(如果它们是ABI 不兼容)。

标签: c binaryfiles fread


【解决方案1】:

那不是二进制文件,是文本文件,试试这个

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *file;
    int   value;
    char  text[33];
    char *endptr;

    file = fopen("file2", "r");
    if (file == NULL) /* check that the file was actually opened */
        return -1;
    if (fread(text, 1, sizeof(text) - 1, f1) != sizeof(text) - 1)
    {
        fclose(file);
        return -1;
    }
    text[32] = '\0';
    value    = strtol(text, &endptr, 2);
    if (*endptr == '\0')
        printf("%d", value);
    fclose(file);
    return 0;
}

要编写二进制文件,你需要这个

#include <stdio.h>

void hexdump(const char *const filename)
{
    FILE         *file;
    unsigned char byte;


    file = fopen(filename, "rb");
    if (file == NULL)
        return;
    fprintf(stdout, "hexdump of `%s': ", filename);
    while (fread(&byte, 1, 1, file) == 1)
        fprintf(stdout, "0x%02x ", byte);
    fprintf(stdout, "\n");
}

int main()
{
    const char *filename;
    FILE       *file;
    int         value;

    filename = "file.bin";
    file     = fopen(filename, "wb");
    if (file == NULL)
        return -1;
    value = 7;
    if (fwrite(&value, sizeof(value), 1, file) != 1)
        fprintf(stderr, "error writing to binary file\n");
    fclose(file);

    /* check that the content of the file is not printable, i.e. not text */
    hexdump(filename);

    file = fopen(filename, "rb");
    if (file == NULL)
        return -1;
    value = 0;
    if (fread(&value, sizeof(value), 1, file) != 1)
        fprintf(stderr, "error writing to binary file\n");
    else
        fprintf(stdout, "The value read was %d\n", value);
    fclose(file);

    return 0;

}

正如您从上面的示例中看到的,file.bin 中存储的数据不是文本格式,您无法使用文本编辑器对其进行检查,因为字节 0x000x07 不可打印,实际上是 @ 987654327@ 是 nul 字节,在 c 中用于标记字符串的结尾。

【讨论】:

  • 但是根据这篇文章,不是所有文件都是二进制文件,直到被告知它们是文本? stackoverflow.com/questions/979816/…\
  • 因为只包含文本的二进制文件等价于对应的文本文件,但是如果您将整数存储在二进制文件中,您将不太可能看到整数值的文本表示,也"rb" AFAIK 仅不像 "r" 那样处理行尾字符。
  • 谢谢你的时间,但我有点困惑。我如何创建一个 32 位/4 字节的二进制文件,就像我描述的那样?
  • @mike 输出了7?
  • @mike 哎呀,它的if (*endptr == '\0') 而不是if (*endptr != '\0')。我修好了,它应该打印7
【解决方案2】:

这是您可以写入和读取二进制文件的一种方式。在读取二进制文件和 ASCII 文件之前,您应该知道它们之间的区别。 阅读一次https://stackoverflow.com/a/28301127/3095460 并了解您正在使用您的代码阅读的文件类型。

如果您在编辑器中打开文件并看到00000000000000000000000000000111,这并不意味着它是二进制文件,大多数编辑器仅将文件处理为 ascii 文本文件。你需要一个二进制编辑器来打开一个二进制文件并从中读取有意义的数据。

#include <stdio.h>

int main()
{
    FILE *Read_fptr  = NULL;
    FILE *Write_fptr = NULL;
    int   data       = 20;
    size_t nElement  = 1;

    if ( (Write_fptr = fopen("data.bin", "wb")) != NULL ) {
        if ( fwrite(data, nElement, sizeof data, Write_fptr) != sizeof data ) {
            fprintf(stderr,"Error: Writing to file\n");
            fclose(Write_fptr);
            return -1;
        }
    } else {
        fprintf(stderr,"Error: opening file for writing\n");
        return -1;
    }
    fclose(Write_fptr);
    data = 0;
    if ( (Read_fptr = fopen("data.bin", "rb")) != NULL ) {
        if ( fread(data, nElement, sizeof data, Read_fptr) != sizeof data) {
            if( !feof(Read_fptr) ) {
                fprintf(stderr,"Error: Reading from file\n");
                fclose(Read_fptr);
                return -1;
            }
        }
    } else {
        fprintf(stderr,"Error: opening file for reading\n");
        return -1;
    }
    fclose(Read_fptr);

    printf("%d\n",data);
    return 0;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    相关资源
    最近更新 更多