0

In recover my code compiles and runs. It then creates 50 images (named correctly) but they do not open and I get a 'invalid or unsupported image format' warning when I try to open them. I have looked through all the questions similar to this and tried everything I could to find the error. All help is greatly appreciated!

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

int main (int argc, char *argv[])
{
if (argc != 2)
{
    fprintf(stderr, "enter two command line arguments\n");
    return 1;
}

char *infile = argv[1];

//if (argv[1] != )
//open memory card
FILE *inptr = fopen( infile , "r");
if (inptr == NULL)
{
    fclose(inptr);
    fprintf(stderr, "Could not open card.raw.\n");
    return 2;
}

//create buffer
unsigned char buffer[512];

//create counter
int counter = 0;
int name = 0;

//current filename and img
char title [10];
FILE* img;

//repeat until end of card
while(fread(&buffer, 512, 1, inptr))
{
    //if start of new JPEG
    if (buffer[0]== 0xff && buffer[1] == 0xd8 &&
    buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {

        if (counter > 1)
        {
            fclose(img);
        }

        sprintf(title, "%03i.jpg", name);

        counter++;
        name++;

        img = fopen(title, "w");

        fwrite(inptr, 512, 1, img);
    }
        //if not first
    if (counter > 0)
    {
        fwrite(&buffer, 512, 1, img);
    }
} 
//close any remaining
fclose(img);
fclose(inptr);

}

Thank you in advance for all help!

2

Look at the following code:

    fwrite(inptr, 512, 1, img);

Why are you writing something back to the input file?

Even if it was supposed to go to the output file, why would the same block be written out twice? Once by this line, and then again by the next fwrite statement?

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

| improve this answer | |

You must log in to answer this question.

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