ok.
practically fread does a simple job, reads a certain amount of bytes from a file (argument 2 * argument 3) and stores them in the specified buffer... something like a memcpy but with the file as source.
so in your case you have buffer which is simply a pointer to nothing (uninitialized) and so when you call fread it put these bytes to a random location (if you used void *buffer = NULL; it tried to put the bytes into 0x00000000).
so you must first allocat the buffer giving it enough space for containing all the bytes so:
unsigned char *buffer;
buffer = malloc(st.st_size);
fread(&buffer, 1, st.st_size, in);
...
free(buffer);
(as you can see I have specified buffer as "unsigned char" and not as "void" because void should be used only for functions and functions arguments).
you could use also alloca() if you want which practically allocates the memory on the stack but obviously it's limited by the stack size so is generally not suggested.
anyway exist a lot of things that you can optimize or just remove in that code.
one of the first rules is NEVER using system(), never never never.
then when you have a #define set it's better to use:
Code:
#ifdef WIN32
dothis,dothat
#endif
instead of if(WIN32) dothis,dothat
and then the:
Code:
for(i=0; i<st.st_size; i++) {
fscanf(buffer, "%c", &tmp);
fprintf(out, "%x", tmp);
}
which is bad 2 times, first because you don't need to use fscanf and then because you print the chars as concatenated %x and so they don't have a fixed size (byte 0x01 occupies 1 char while 0x10 occupies 2) so it's better to use:
Code:
for(i=0; i<st.st_size; i++) {
fprintf(out, "%02x", buffer[i]);
}
there are also other things but you will have fun to find and resolve them :)
oh remember EVER to improve the verbosity of your compiler (more warnings) and being sure that it compiles without warnings