0

So I'm nearly finished with recover. It recovers 50 jpegs but I can't open any of them. I just can't seem to figure out what is causing the issue looking through my code. Any help would be appreciated

int main(int argc, char *argv[1])
{
// ensure proper usage
if (argc != 2)
{
    fprintf(stderr, "Usage: argc must = 2\n");
    return 1;
}

//remember file name
char *infile = argv[1];

//open input file
FILE *inptr = fopen(infile, "r");
if(inptr == NULL)
{
    fprintf(stderr, "Could not open %s\n", argv[1]);
    return 2;
}

//create new buffer
uint8_t buffer[512];

//create output file
FILE *img = NULL;

//filename buffer
char filename[8];

int counter = 0;

//iterate over file 512 times 1 bytes at a time until EOF
while(fread(buffer, 1, 512, inptr) != 0x00)
{
    //check to see if we have a jpeg
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && 
(buffer[3] & 0xf0) == 0xe0)
    {
        //if this is the first jpg
        if(img == NULL)
        {

            //create jpeg file
            sprintf(filename, "%03i.jpg", counter);
            counter++;

            //open the new jpg
            img = fopen(filename, "w");

            if(img == NULL)
            {
                fprintf(stderr, "could not create new jpeg");
                return 3;
            }

            //write jpeg to newly created img
            fwrite(&inptr, 512, 1, img);
        }

        //if not first jpeg
        else
        {
            //close last jpeg
            fclose(img);

            //create jpeg file
            sprintf(filename, "%03i.jpg", counter);
            counter++;

            //open the new jpeg
            img = fopen(filename, "w");

            if(img == NULL)
            {
                fprintf(stderr, "could not create new jpeg");
                return 3;
            }

            //write jpeg to newly created img
            fwrite(&inptr, 512, 1, img);
        }
    }
    //if jpeg header not found
    else
    {
        if(img != NULL)
        {
            //write jpeg to newly created img
            fwrite(&inptr, 512, 1, img);
        }
    }

}

fclose(img);
fclose(inptr);
}
1

Hmmmm.....

        fwrite(&inptr, 512, 1, img);

What's the name of your buffer?

BTW, in case you were thinking about it, fwrite will not write directly from one file to another. It only writes from memory to a file. Because of that, it's actually reading 512 bytes from the pointer var's location in memory. Also, even if it did, the code above would result in two sequential reads from the input file for each write to the output file, resulting in half the data being lost.

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

| improve this answer | |
  • Thanks for the reply, I get what you are saying and have fixed it. However, now I get a "signed integer overflow" error when I try to compile. Tried reverting my code back to what it was just to see if it would work and it no longer works either. strange. – Joe Joe Joe Apr 25 '18 at 19:17
  • Signed integer overflow? I'd look for an infinite loop. BTW, when I tested your code, I just fixed the fwrite statements and it worked fine. – Cliff B Apr 25 '18 at 20:00
  • My bad, it wasn't actually an error message (duh!) I just thought it was because the compiler said "signed integer overflow" and then "Werror", but it's not an error, it compiled fine. :) – Joe Joe Joe Apr 25 '18 at 22:33

You must log in to answer this question.

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