It's segfaulting on your fclose(outptr);
because your outptr is null, probably, given that you have never actually opened one.
Remember that the bytes aren't of type char
. They are an 8 bit unsigned type (an unsigned char
) so your array should be created with that type. Also, atoi()
is not the correct function here, as that will always return 0. Simply compare the byte to its appropriate hex value, like if(buffer[0] == 0xff)
etc.
Also, sizeof(inptr)
is the size of the pointer (i.e., 4 bytes) so your loop will only run 4 times. This is another reason why you never found a jpg.
edit
This:
char* outName = "";
sprintf(outName, "0%i%i.jpg", tens, ones);
You are declaring a string literal when you set a char* to a value using ""
. By definition, this variable is fixed and trying to write to it will result in a segfault.
Change that to a char array rather than a char* (making sure to make the array long enough to hold all the chars in your outName including the nul-terminator.)
Also, how are you determining the size of the card.raw file? You cannot hardcode its size into your program as there is no guarantee that you will be given the exact same one another time. You need to figure out how to determine if you've read everything. Perhaps consider using the return value of your fread()
as your loop condition.