因此,我尝试读取每个块包含4个字节的二进制文件,并且在对文件进行十六进制转储时,输出为:
0000000 0022 0000 6261 6463 3030 3030 6261 64630000010 3030 3030 6261 6463 3030 3030 6261 6463*00000d0 3030 3030 6261 6463 3030 3030 000a00000dd
(抱歉,每行数字之间不应有一行)

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

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是无符号整数的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。您可能需要考虑一下同样在你的情况下。因此,就我而言,它运行良好。

关于c - 用C读取一个二进制文件,然后将文件中的数据与我已读入的文件进行匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28239081/

10-09 00:15