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

在C中读入二进制文件,然后将文件中的数据与我读入的数据进行匹配,c,binaryfiles,C,Binaryfiles,所以我尝试读取一个二进制文件,每个块有4个字节,当对我的文件执行hextump时,这里是输出: 0000000 0022 0000 6261 6463 3030 3030 6261 6463 0000010 3030 3030 6261 6463 3030 3030 6261 6463 * 00000 D0 3030 3030 6261 6463 3030 3030 000a 00000日 (对不起,每行数字之间不应该有一行) 但当它将这些值读入数组时,我得到的输出是: 34 6100000

所以我尝试读取一个二进制文件,每个块有4个字节,当对我的文件执行hextump时,这里是输出:

0000000 0022 0000 6261 6463 3030 3030 6261 6463
0000010 3030 3030 6261 6463 3030 3030 6261 6463
*
00000 D0 3030 3030 6261 6463 3030 3030 000a
00000日
(对不起,每行数字之间不应该有一行)

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

34 61000000 30646362 61303030 30646362 61303030 30646362 61303030 30646362 2283030 44c5bbc0 7fff 22 0 14fea515 7f68 0 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;
}    
#包括
#包括
#包括
int main(int argc,字符**argv)
{
无符号字符缓冲区[1];
文件*文件指针;
filePointer=fopen(argv[1],“rb”);
//无符号字符缓冲区[filePointer];
int readVal=0;
readVal=fread(buffer,sizeof(buffer),1,filePointer);
如果(readVal==0)
{
printf(“文件未读取任何内容\n”);
}
int size=缓冲区[0];
/*int i=1;
对于(;i<1;i++)
{
printf(“%x”,缓冲区[i]);
}
*/
//int size=缓冲区[0];
printf(“%d\n”,大小);
无符号整数数组[大小];
int其他数据元素;
otherDataElements=fread(数组、大小(缓冲区)、大小、文件指针);
if(otherDataElements==0)
{
printf(“那里什么都没有!”;
}
int j=0;
对于(;j

有一些代码被注释掉了,忽略它。谢谢

什么是
尺寸
?字节数?还是整数

这似乎是可疑的:

otherDataElements = fread(array, sizeof(buffer), size, filePointer);
尝试更改为:

otherDataElements = fread(array, sizeof(unsigned int), size/sizeof(unsigned int), filePointer);
因为数组是无符号整数的
array
,而
fread
的第二个参数要求您读取每个元素的大小。如果您想将文件内容解释为4字节整数的数组,那么文件长度也应该是4的倍数

无论如何,这个程序在我的机器上的输出是(这是一个类似于你刚才添加的写入数据到文件中进行测试的程序-我简化了读取):

您可以看到输出被交换,但这是因为endianness-当我的计算机将字节00 01 02 03解释为整数的字节时,它是以小endian的方式进行的-因此输出是整数03020100。你也可以在你的情况下考虑这一点。所以在我的情况下,我想效果不错

#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