简体   繁体   English

从二进制文件读取到int

[英]reading from a binary file into an int

i'm a little puzzled about binary files and how to read from them, so if someone could help that would be great. 我对二进制文件以及如何从它们中读取有点困惑,所以如果有人可以提供帮助那就太棒了。 i have a file that contains the following bits: 我有一个包含以下位的文件:

00000000000000000000000000000111 

(32 bits. i counted) (32位。我算了)

now i have written this code: 现在我写了这段代码:

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

}

that prints me 808464432. why? 那打印我808464432.为什么? shouldnt it print 7? 不应该打印7? thank you for reading. 谢谢你的阅读。

That is not a binary file, it's a text file, try this instead 那不是二进制文件,它是一个文本文件,请尝试这样做

#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;
}

To write a binary file you need this 要编写二进制文件,您需要这样做

#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;

}

as you will see from the example above, the stored data in file.bin is not in text format, you cannot inspect it with a text editor because the bytes 0x00 and 0x07 are not printable, in fact 0x00 is the nul byte which is used in c to mark the end of a string. 正如您将从上面的示例中看到的那样, file.bin存储的数据不是文本格式,您无法使用文本编辑器检查它,因为字节0x000x07不可打印,实际上0x00是使用的nul字节在c中标记一个字符串的结尾。

This is one way you can write and read to a binary file. 这是您可以写入和读取二进制文件的一种方法。 you should know the difference between a binary and ASCII file, before reading from them. 在读取它们之前,你应该知道二进制文件和ASCII文件之间的区别。 Read this once https://stackoverflow.com/a/28301127/3095460 and understand what is type of file you are reading using your code. 阅读此内容https://stackoverflow.com/a/28301127/3095460并了解您正在使用代码阅读的文件类型。

if you open a file in an editor and see 00000000000000000000000000000111 it does not mean its a binary file, most of the editor process files as ascii text file only. 如果你在编辑器中打开一个文件并看到00000000000000000000000000000111它并不意味着它是一个二进制文件,大多数编辑器处理文件只作为ascii文本文件。 you need a binary editor to open a binary file and read meaningful data from them. 您需要一个二进制编辑器来打开二进制文件并从中读取有意义的数据。

#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;

}

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

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