Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read binary file, save in buffer, print out content of buffer

I have a big problem that need's to be solved before I can continue with my program.

I have to open a binary file, read it's content, save the content into a buffer, allocate space on the heap with malloc, close the file and finally printf( the content of the .bin file). I came this far (closing file is not implemented yet):

void executeFile(char *path){
    FILE *fp; /*filepointer*/
    size_t size; /*filesize*/
    unsigned int buffer []; /*buffer*/

    fp = fopen(path,"rb"); /*open file*/
    fseek(fp, 0, SEEK_END); 
    size = ftell(fp);         /*calc the size needed*/
    fseek(fp, 0, SEEK_SET); 
    buffer = malloc(size);  /*allocalte space on heap*/

    if (fp == NULL){ /*ERROR detection if file == empty*/
        printf("Error: There was an Error reading the file %s \n", path);           
        exit(1);
    }
    else if (fread(&buffer, sizeof(unsigned int), size, fp) != size){ /* if count of read bytes != calculated size of .bin file -> ERROR*/
        printf("Error: There was an Error reading the file %s - %d\n", path, r);
        exit(1);
    }else{int i;
        for(i=0; i<size;i++){       
            printf("%x", buffer[i]);
        }
    }
}

I think I messed up the buffer and I am not really sure if I read the .bin file correctly because I can't print it with printf("%x", buffer[i])

Hope you guys can help

Greetings from germany :)

like image 443
user2418126 Avatar asked May 24 '13 15:05

user2418126


People also ask

How do you extract data from a binary file?

To read from a binary fileUse the ReadAllBytes method, which returns the contents of a file as a byte array.

How do I open a binary file in read mode?

To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable.

How to read whole binary file in c?

Steps To Read A File:Open a file using the function fopen() and store the reference of the file in a FILE pointer. Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or fread(). File close the file using the function fclose().

How do I view BytesIO files?

BytesIO simulates a file stream from a buffer. if you just want the byte data, data = f. read() is all you need.


1 Answers

Recommended changes:

  1. Change your buffer to a char (byte), as the ftell() will report the size in bytes (char) and malloc() use the byte size also.

    unsigned int buffer []; /buffer/

to

unsigned char *buffer; /*buffer*/
  1. [Edit] 2021: Omit cast
    2) This is OK, size is bytes and buffer points to bytes, but could be explicitly cast

    buffer = malloc(size); /allocate space on heap/

to

buffer = (unsigned char *) malloc(size);  /*allocate space on heap*/
/* or for those who recommend no casting on malloc() */
buffer = malloc(size);  /*allocate space on heap*/
  1. change 2nd parameter from sizeof(unsigned int) to sizeof *buffer, which is 1.

    else if (fread(buffer, sizeof(unsigned int), size, fp) != size){

to

else if (fread(buffer, sizeof *buffer, size, fp) != size){ 
  1. Change "%x" to "%02x" else single digit hexadecimal numbers will confuse the output. E. g. is "1234" four bytes or two?

    printf("%x", buffer[i]);

to

printf("%02x", buffer[i]);
  1. Your clean-up at the end of the function may include

    fclose(fp); free(buffer);

like image 163
chux - Reinstate Monica Avatar answered Sep 20 '22 08:09

chux - Reinstate Monica