0

I can't figure out what's wrong in my code. I'm getting 49 blank images along with gibberish files with jpg extension like below. Please drop a comment if anything is unclear in my code.

Edit: FILE *imagefile = fopen(fname, "a") seems to be creating a new jpg then appending to it instead of appending to already existing file. I don't know why it is acting this way.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Wrong args");
        return 1;
    }

    char *name = argv[1];

    FILE *inpfile = fopen(name, "r");

    if (!inpfile)
    {
        fprintf(stderr, "not found");
        return 2;
    }

    unsigned char block[512];
    int i = 0;
    int open = 0;

    while(fread(block, 512, 1, inpfile))
    {
        char fname[7];

        if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
        {
            sprintf(fname, "%03i.jpg", i);
            FILE *imagefile = fopen(fname, "w");
            fwrite(block, 512, 1, imagefile);
            fclose(imagefile);
            open = 1;
            ++i;
        }
        else if (open)
        {
            FILE *imagefile = fopen(fname, "a");
            fwrite(block, 512, 1, imagefile);
            fclose(imagefile);
        }
    }

}

The open signals the start of the recovery.

1

The double file names are a side effect of an earlier issue. Let's look at that.

char fname[7];
...
        sprintf(fname, "%03i.jpg", i);
        FILE *imagefile = fopen(fname, "w");

The fname var is used to build the file name as a string. Each file name is 7 chars - a 3 digit number, followed by .jpg. The problem here is that a string still needs to be terminated with the end of string marker, \0. By allocating 7 chars to fname, it didn't leave room for the EOS marker. This leads to unpredictable results, as you have seen. It needs to be allocated with 8 chars.

Next, let's talk about the other cause of two files being opened. Your assessment that the second fopen is creating the second file is correct, although I can't explain why this happens. Having said that, I have a more fundamental question. You have chosen to open and close a file every time it is accessed. This is terribly inefficient. (The overhead in resources, particularly time, for a file open or close operation is much larger than a read or a write.) Also, when opening a file for append, it will append to an existing file, or, if the file doesn't exist, it will create it. This could lead to problems in itself.

Instead, it is only necessary to open the file in w or write mode when a signature is found, and leave it open while continuing to process data. The file is only then closed when a new signature is found. This will dramatically cut processing time, although it is hard to see with such a short run time now.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Gotcha! That sneaky /0. Second time the file is opened,fname carries some garbage character and program can't find it on the disk. Besides I rewrote it to be more efficient now. Thanks a lot.
    – tejasvi88
    Sep 23 '19 at 1:56

You must log in to answer this question.

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