I've been having trouble with pset3's Recover exercise. My code successfully compiles and creates 50 jpegs, numbered 000 to 049. However, all images are terribly glitchy. Examples:
I've scoured the other Recover-related questions in here and tried checking for the bugs other users have found:
- Accidentally calling
fread()
twice in a row; - Getting images of the same size when running
ls -al *.jpg
; - Using
fopen()
improperly; - Failing to properly check first 4 bytes of header;
- etc.
I have a feeling that maybe the structure of my program is calling some functions in the wrong order, but I've been looking at this code (and running debug50) for hours and still couldn't figure this out.
Could someone give me a hint of what's wrong? Here's the relevant code (edited after question was answered to show only the buggy part):
...
// Name for new file
char *filename[8];
sprintf(*filename, "%03i.jpg", imageCount);
imageCount++;
// Open new file
FILE *imageptr = fopen(*filename, "w");
// Write header
fwrite(&input, 1, 512, imageptr);
// Read further
fread(&input, 1, 512, cardptr);
...
Thanks in advance!
Edit: Thanks to curiouswiki and Blauelf's answers, I got the code working!
In earlier versions of the program I'd had problems with filename
's scope. I tried to solve that using pointers, but went about it the wrong way. After solving my scope problems with the program's structure, I kept the char *filename[8]
declaration thinking it was giving me a string and "hey, this couldn't possibly be the wrong line of code..."