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