EDIT: Okay, I appear to have found the problem. I was actually opening the file pointer when I declared FILE *outptr, instead of setting it to NULL. (I should have shown that bit of code...) That said, the condition to check if it hit a jpeg header should have closed the outptr file, although the write command still seemed to do something (here, create junk data on the disk). Is this an example of 'undefined' behaviour?
My program recovers all the jpegs correctly, and also passes the check50 test.
However, each time I run it, it is also creating junk files/data in my /recover directory. Weirdly, these don't seem to be any kind of actual file - when I go to delete them, the IDE says that they aren't even files! Viewing them in the hex editor indicates that they are mostly the same, but not entirely.
The relevant code in terms of scanning the .raw file:
while (1 < 2) {
// read file (struct, size of element, number of elements to read, file pointer)
if (fread(&buffer, 512, 1, inptr) != 1) {
break;
}
// if it's a jpeg header...
if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)) {
jpg_count += 1;
// close previous jpeg file if open
if (jpg_count > -1) {
fclose(outptr);
}
// create new filename
sprintf(outfile, "%03i.jpg", jpg_count);
// open a new jpeg file
fopen(outfile, "w");
}
if (jpg_count > -1) {
// write the bytes of the jpeg
fwrite(buffer, 512, 1, outptr);
}
}
// close file
fclose(outptr);
fclose(inptr);
return 0;
// end while loop
}
What are these files? What can I do to fix this?
fopen
somewhere? And you forgot theoutptr =
in front of thefopen(outfile, "w")
. Also, there must be something wrong with yourjpg_count
. What's its initial value? And what is its value after incrementing, is it ever possible for theif (jpg_count > -1)
to be false at that point?ls -al
from the command line in the recover directory to see the date/time stamp on the files.FILE *
, correct?) So, when you fclose outptr, it is attempting to close a file using random data. (and I need to stop commenting on questions in the middle of the night! :-O)