0

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.

strange files

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?

6
  • Is outfile declared with a length of 7 or 8? Also, why do you have both outfile and outptr?
    – Cliff B
    Mar 31, 2017 at 9:18
  • Length 8, and outfile is just the string to name the jpeg files with (is there a better way to to that?). The name of the variable is, however, bad, and already changed.
    – Alex L
    Mar 31, 2017 at 9:35
  • 1
    Maybe another fopen somewhere? And you forgot the outptr = in front of the fopen(outfile, "w"). Also, there must be something wrong with your jpg_count. What's its initial value? And what is its value after incrementing, is it ever possible for the if (jpg_count > -1) to be false at that point?
    – Blauelf
    Mar 31, 2017 at 11:54
  • Perhaps they are not being created every time you run recover. Are they artifacts from when the program was buggy? Since you can't delete them, they are "still" (always?) there. You can do ls -al from the command line in the recover directory to see the date/time stamp on the files. Mar 31, 2017 at 12:31
  • 1
    Blauelf is on target. The code never assigns a file to the file pointer outptr. (You did declare it as a pointer with 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)
    – Cliff B
    Mar 31, 2017 at 17:54

0

You must log in to answer this question.

Browse other questions tagged .