0

I want to read from few parts of a file and than output it as one unsigned char. This is a simplified version of this:

void loadPartsOfFile (const char *filename, unsigned char **output)
{
    *output = malloc(333);

    FILE *file = fopen(filename, "rb");
    fseek(file, 0, SEEK_SET); 
    fread(*output, 1, 111, file);
    fseek(file, 10254, SEEK_SET);
    fread(*output, 1, 222, file);

    fclose(file);
}

Second fread just overwrites what first added to output. Is there a way to append second data stream to output?

Martin
  • 145
  • 6

2 Answers2

2
void loadPartsOfFile (const char *filename, unsigned char **output)
{
    *output = realloc(*output, 333);

    FILE *file = fopen(filename, "rb");
    fseek(file, 0, SEEK_SET); 
    fread(*output, 1, 111, file);
    fseek(file, 10254, SEEK_SET);
    fread(*output+111, 1, 222, file);

    fclose(file);
}
wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • Thank you! By the way is there a way to add to output some data like: char data[2] = {0xD5,0x48}; – Martin Apr 15 '12 at 21:41
  • Yes, of course. Just increment the size for realloc by 2, and do a `memcpy(*output+some_offset, data, 2);` somewhere. If the data is inserted before the other two members, these will have to be shifted up too. – wildplasser Apr 16 '12 at 10:22
2

Just increment *output until the end of the previous read, i. e. *output + 111.

Sebastian
  • 8,046
  • 2
  • 34
  • 58