0

The images recovered correctly but my program did not pass 'check50'.
I posted the query earlier too but did not get a satisfactory answer.
I am desperately looking for an answer so please help I have been on this problem for a pretty long time now.

The code is:

FILE* infile = fopen("card.raw", "r");

// detect the first jpeg
unsigned char jpeg0[5] = {0xff, 0xd8, 0xff, 0xe0, 0xef};

// open new jpeg
unsigned char buffer[512];

fread(buffer, 1, 512, infile);

int filecount = 0;
int flag = 0;
FILE* outfile;
do
{
    // write 512 bytes until a new jpeg is detected
    if (buffer[0] == jpeg0[0] && 
        buffer[1] == jpeg0[1] && 
        buffer[2] == jpeg0[2] && 
        (buffer[3] >= jpeg0[3] && 
        buffer[3] <= jpeg0[4]))
    {
        if (flag == 0)
        {
            flag = 1;
        }
        else
        {
            fclose(outfile);
        }
        char fname[8];
        sprintf(fname, "%03d.jpg", filecount);
        outfile = fopen(fname, "w");
        filecount++;
    }

    fwrite(buffer, 512, 1, outfile);
    // find end of file
}while(fread(buffer, 1, 512, infile) == 512);

fclose(outfile);
fclose(infile);

return 0;

In addition to this there were some interesting observations and I am searching their answer too. When I run the program, some times it shows me segmentation fault and at other times it does not. Why?

enter image description here

1

Actually, the last answer appears to have been very satisfactory. I see that you adopted the suggestions and incorporated them into your code and did quite well. Having said that, there is one remaining, new problem with two related root causes.

You are getting a seg fault at the fwrite near the bottom of the file. This is happening because outfile has yet to be opened, yet the code is trying to write to it. The file hasn't been opened (and shouldn't be) because the code is still processing garbage data at the start of the file. This is further complicated because outfile was never initialized. Since it won't be initialized by default, it contains garbage data - not a valid pointer to a file nor NULL.

The outfile pointer needs to be initialized and the fwrite needs to be set up to only execute when a file is opened. That should be all that remains to get it working.

I still don't understand why you were getting intermittent seg faults. I was getting 100% seg faults. Perhaps it's a difference in environments. It will probably remain a mystery forever. ;-)

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

  • I don't understand why he said your answer wasn't satisfactory when he's clearly implemented all that was pointed out there! haha Even I gave an upvote to that answer because I found it very helpful to others with the same problems as him. – Yuri Laguardia Nov 7 '16 at 22:09
  • that was because my program did not pass check50 even after i implemented the code you offered. And yes, it was helpful if not all i wanted – RISHABH KALAKOTI Nov 8 '16 at 9:35
  • sorry. The mistake was mine. It worked. Now finally, I am able to pass check50. It was just that I ignored my mistake as i was not getting 100% seg fault. Still a mystery... Thank You! – RISHABH KALAKOTI Nov 8 '16 at 9:41

You must log in to answer this question.

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