在 C 中读入二进制文件,然后将文件中的数据与我读入的内容进行匹配

Reading in a binary file in C, then matching data from the file to what I have read in

所以我试图读入一个二进制文件,每个块有 4 个字节,当对我的文件进行 hexdump 时,输出如下: 0000000 0022 0000 6261 6463 3030 3030 6261 6463 0000010 3030 3030 6261 6463 3030 3030 6261 6463 * 00000d0 3030 3030 6261 6463 3030 3030 000a 00000dd (每行数字之间不应该有一行,抱歉)

但是当它将这些值读入数组时,我得到的输出是:

34 61000000 30646362 61303030 30646362 61303030 30646362 61303030 30646362 2283030 0 44c5bbc0 7fff 22 0 14fea515 7f68 0 0 22 0 22 0 4007b2 0 1 0 1000 0 2289010 0 44c5bbc0 7f

我已经按照网上的很多说明进行操作,但我真的不确定从这里该何去何从。

这是我使用的代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char** argv)
{
    unsigned char buffer[1];
    FILE *filePointer;
    filePointer = fopen(argv[1], "rb");

    //unsigned char buffer[filePointer];

    int readVal = 0;

    readVal = fread(buffer, sizeof(buffer), 1, filePointer);
    if (readVal == 0)
    {
            printf("File did not read anything\n");
    }

    int size = buffer[0];

    /*int i = 1;    

    for(; i < 1; i++)
    {
            printf("%x ", buffer[i]);
    }

    */
    //int size = buffer[0];
    printf("%d\n", size);
    unsigned int array[size];
    int otherDataElements;

    otherDataElements = fread(array, sizeof(buffer), size, filePointer);

    if (otherDataElements == 0)
    {
            printf("There was nothing there!");
    }

    int j = 0;
    for (; j < size; j++)
    {
            printf("%x ", array[j]);
    }
    return 0;
}    

部分代码被注释掉,忽略。谢谢!

什么是size?字节数?还是整数?

这看起来很可疑:

otherDataElements = fread(array, sizeof(buffer), size, filePointer);

试试改成这样:

otherDataElements = fread(array, sizeof(unsigned int), size/sizeof(unsigned int), filePointer);

因为数组是 array 个无符号整数,fread 的第二个参数要求您提供要读取的每个元素的大小。如果你想将文件内容解释为我认为的 4 字节整数数组,文件长度也应该是 4 的倍数。

无论如何,这个程序在我的机器上的输出是(这与你的程序类似,只是将写入数据添加到文件以进行测试 - 我已经简化了阅读):

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

void test()
{
    unsigned char buffer[1];
    FILE *filePointer;
    filePointer = fopen("/Users/macbookair/Documents/test/test/t.bin", "rb");

    //unsigned char buffer[filePointer];


    //readVal = fread(buffer, sizeof(buffer), 1, filePointer);
    //if (readVal == 0)
    //{
    //     printf("File did not read anything\n");
    // }

    //int size = 4;

    /*int i = 1;

     for(; i < 1; i++)
     {
     printf("%x ", buffer[i]);
     }

     */
    //int size = buffer[0];
    //printf("%d\n", size);
    unsigned int array[4] = {0};
    int otherDataElements;
    int nrOfInts = 1; // In this test method we only want to read 1 integer from file, we assume there is one integer only

    otherDataElements = fread(array, sizeof(unsigned int), nrOfInts, filePointer);

    if (otherDataElements == 0)
    {
        printf("There was nothing there!");
    }

    int j = 0;
    for (; j < nrOfInts; j++)
    {
        printf("%x ", array[j]);
    }
}


int main(int argc, const char * argv[])
{
    // Just do a test write to file. Write 4 bytes to read back later.
    FILE *write_ptr;
    unsigned char buffer[4] = {0,1,2,3};
    write_ptr = fopen("/Users/macbookair/Documents/test/test/t.bin","wb");   

    fwrite(buffer,sizeof(buffer),1,write_ptr);  
    fclose(write_ptr);

    // Call our method for reading the data
    test();
    return 0;
}

这个:

3020100 

您可以看到输出被交换了,但那是因为字节顺序——当我的计算机将字节 00 01 02 03 解释为整数字节时,它以小端方式进行——因此输出是整数 03020100。你在您的情况下也可能要考虑这一点。所以就我而言,我猜它工作得很好。