0

Hello everyone so this is my code, it does compile and writes the recovers the first photo (that is 000.jpeg) but when it comes to continuing to the new jpeg it can only write one block(512BYTE) once and then stops. I am looking for a condition for it to go but isjpeg() function is not operable since it holds the same beginning (not sure why but tried does not work). I am looking for a way of continuation for it. Thanks all help appreciated.

(This is the output on terminal so that you see the result,Number of I could not found new is reduced for easier demonstration.)

$ ./recover card.raw
I could not found a new so keep goin
I could not found a new so keep goin
I can found the first jpeg
I could not found a new so keep goin
I could not found a new so keep goin
I could not found a new so keep goin
I could not found a new so keep goin
I could not found a new so keep goin
I could not found a new so keep goin
I can write to the new jpeg

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef unsigned char BYTE;
int main(int argc, char *argv[])
{
    int jpegnumber; jpegnumber = 0;
    bool isjpeg();
    bool jpeghasfound = false;
    char filename[8];

    if (argc != 2 )
    {
        printf("Program should have one command line argument \n");
        return 1;
    }

    FILE *file = fopen(argv[1], "r");

    if (file == NULL)
        return 1;

    BYTE buffer[512];

    FILE* img;

    while (fread(buffer, 512, 1, file)) // if it reads something
    {
        if ( isjpeg(buffer) && (!jpeghasfound) ) // it founds a jpeg first time
        {
            jpeghasfound = true;

            sprintf(filename,"%03i.jpg",jpegnumber);

            img = fopen(filename,"w");
            printf("I can found the first jpeg\n\n");
            if (img == NULL)//nullcheck
            {
                printf("There is nothing to open \n");
                return 1;
            }
            fwrite(buffer,512,1,img);

            jpegnumber++; // increment the count
        }

        else if (isjpeg(buffer) && jpeghasfound)  // Founding a new jpeg
            {
                fclose(file); // close old file
                sprintf(filename,"%03i.jpg",jpegnumber);
                if (img == NULL)//nullcheck
                {
                    printf("There is nothing to open \n");
                    return 1;
                }
                    fwrite(buffer,512,1,img);
                    printf("I can write to the new jpeg\n");
            }
        else  // if we have not find a new jpeg we should keep writing to old file
        {
            fwrite(buffer,512,1,img);
            printf("I could not found a new so keep goin\n");
        }

    }
    fclose(file);
    fclose(img);
}

bool isjpeg(BYTE buffer[])
{

        if((buffer[0] == 0xff ) && (buffer[1] == 0xd8 ) && (buffer[2] == 0xff ) && ((buffer[3] & 0xf0 ) == 0xe0))
        {
            return true;
        }
        else
        {
            return false;
        }
}
0

this is the problem. If file is not NULL, return 1. It should be the other way around.

 if (file != NULL)
    return 1;

Even if you fix this one, your code still have other memory issues. You can see it here, img is not a valid file pointer.

3
  • I have changed my way of asking the problem a little I would be glad if you can take a look again @stensal – IDK May 25 '20 at 17:17
  • I will take a look ASAP. – stensal May 25 '20 at 20:24
  • I was able to do it thanks :) @stensal – IDK May 25 '20 at 20:45

You must log in to answer this question.

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