2015-01-30 5 views
0

그래서 청크 당 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 C에서 이진 파일을 읽은 다음 파일에서 읽은 데이터와 일치합니다.

(죄송 자리의 각 라인 사이의 라인이 없어야한다)하지만 출력을 여기에 배열로이 값을 읽을 때 내가 갖는 :

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

, 그것을 무시 : 여기

내가 사용하고 코드입니다. 감사!

답변

0

무엇이 size입니까? 바이트 수? 또는 정수?

이 의심스러운 :

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

보십시오이로 변경 : 배열 부호없는 정수의 array하고 fread 두 번째 매개 변수를 읽을 수 있도록 각 요소의 크기를 묻는

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

때문입니다. 필자가 생각하는 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 
-

어쨌든 내 컴퓨터에이 프로그램의 출력은 (나는 독서를 단순화 당신은 단지 테스트를 위해 파일에 데이터를 쓰고 추가로이 유사한 프로그램)입니다

endianness 때문에 출력이 바뀌 었음을 알 수 있습니다. 컴퓨터가 바이트 00 01 02 03을 정수 바이트로 해석하면 리틀 엔디안 방식으로 출력되므로 정수 03020100이됩니다. 귀하의 경우에도 이것을 고려하십시오. 그래서 제 경우에는 괜찮 았을 것 같아요.

관련 문제