0

So i struggled probably a little less than some did with recover.c, and was able to fix my problems with the help of other's questions on this stackexchange, but although i was able to get my code running properly, my fixes came with little understanding of what was actually happening under the hood, so I was hoping someone here might be able to offer some insight.

The first problem I was encountering was an error message after running my code. Although I successfully generated (opened, wrote to, and closed) all 50 files, when I tried to open them in the CS50 IDE or on my local device, I always got the error message "invalid or unsupported image format". The error was that in the char * array where I saved my filename, i'd only made space for 3 characters. After adjusting the array to be the correct number of characters, the I no longer recieved this error message. The part that confused me, was that files that I generated had the correct name (e.g. "001.jpg") even though they were not stored correctly initially. Anyone have any idea what was going on there?

My second question has to do with the fwrite() function. Initially, I made the mistake of writing fwrite(buffer, MBLOCK * sizeof(BYTES), 1, inptr) lines to my output files. Even though I had made this mistake, i was successful in generating my jpegs. My question is why it still worked? Im assuming it was because i had only declared my buffer space to be 512 bytes long, so after writing everything from the buffer, fwrite automatically identified that there was nothing left to write from buffer and continued as normal? Or was something else happening?

Thanks so much ahead of time, Jonathan

0

Regarding the file name, what might have happened is that the sprintf overwrote part of the data (the beginning of your 512 bytes block) with ".jpg", creating an invalid JPEG header.

Regarding second part, I'm guessing what buffer is, but what is MBLOCK and BYTES? fwrite has no idea of the size of buffer, you have to tell it how many bytes to write.

2
  • I wanted to reply directly here, but it formated very poorly, so my reply is in an answer below
    – ItsAnApe
    Apr 10 '17 at 12:43
  • You should better add it to your question rather than as another answer. Regarding the buffer, it's of the wrong type. You declared an array of 512 pointers to char, while you should have used something like BYTE buffer[MBLOCK]; or uint8_t buffer[MBLOCK];, on most systems also char buffer[MBLOCK]; would be the same. The allocated space is bigger than you would need, so using only the first 512 bytes works. The MBLOCK * sizeof(BYTE) is 512, so the correct size, though sizeof(buffer) might have been more readable (if you had declared buffer right).
    – Blauelf
    Apr 10 '17 at 13:14
0

Sorry, I could have/should have written code to accompany my questions. This is abreviated, but includes all the relevant information:

#typedef uint8_t BYTE;
#define MBLOCK 512

char *buffer[MBLOCK];

fwrite(buffer, MBLOCK * sizeof(BYTE), 1, inptr)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .