0

i have spent some time looking and trying all that i can to make sure i get the 16 pictures from the file given but am only able to recover 15, any help will be appreciated. my code is:

typedef uint8_t BYTE;
//prototype to make jpeg
int create_jpg();
FILE* inptr = NULL;
BYTE buffer [512];

int main(int argc, char* argv[])
{
// TODO
//open file
inptr = fopen("card.raw", "r");
if(inptr == NULL)
{
    printf("Could not open file\n");
    return 2;
}



if(buffer == NULL)
{
    printf("Memory error\n");
    return 3;
}


while(fread(&buffer,512,1,inptr) != 0)
{

fread(&buffer,512,1,inptr);

    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
    {
         create_jpg();
    }
}

return 0;
}
//create jpegs
int create_jpg()
{

char name[4];
int file_num = 0;
//jpg file
FILE* jpg = NULL;

while(!feof(inptr))
{
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
    {
        sprintf(name,"%03i.jpg",file_num);

        if(jpg == NULL)
        {
            //create first jpeg
            jpg = fopen(name,"w+");
        }
        else
        {
            //close file in use
            fclose(jpg);
            //increase the file count
            file_num++;
            //create new file       
            jpg = fopen(name,"w+");

        }

        if(jpg == NULL)
        {
            printf("File creation error\n");
            return 4; 
        }


    }
    //write buffer data to file
    fwrite(&buffer, 512, 1, jpg);

     //read data from card file
     fread(&buffer,512,1,inptr);    


}
//close last jpg
fclose(jpg);
return 0;
}
4
  • as a side comment, it's fortunate that the first block isn't a signature block. You would have skipped it with the double read at the top of the while loop. – Cliff B Oct 6 '15 at 0:03
  • @CliffB Thanks for the answer and the comment. i changed my code to use !feof(inptr). – Luwii Oct 6 '15 at 1:08
  • That would probably work, except what happens when the fread actually hits the eof? Doesn't the code have to loop around to terminate? Wouldn't it be better to simply eliminate the second read and rely on the one in the while loop statement? – Cliff B Oct 6 '15 at 1:21
  • yea it better. thanks for all the help. – Luwii Oct 7 '15 at 18:54
1

The code is overwriting the first file with the second and continuing from there. This is happening because something was overlooked.

    if(jpg == NULL)
    {
        //create first jpeg
        jpg = fopen(name,"w+");
    }
    else
    ...

This is how you create the first file. Unfortunately, you forgot to increment file_num after opening the file. The effect is that the next file created/opened uses the same name and overwrites the first file.

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

1
  • Dont know how i had missed this,Thanks for the help. – Luwii Oct 6 '15 at 1:11

You must log in to answer this question.

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