0

fellow programmers!

I've been stuck for days trying to solve this problem, but i can't really seem to find the error in this code, which opens all the images but they do not match when i use the check50 command although it creates all the image files and iterates correctly. I'd be very grateful if someone could help me out :)

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{



    if (argc != 2)
    {
        printf("Usage: ./recover image");
        return 1;
    }

//if the command line arguments are valid, we open the memory card


    FILE *f = fopen(argv[1], "rb");

//Tests if the file was opened successfully

    if(f == NULL)
    {
        printf("./recover card.raw");
        return 1;
    }


    FILE *img = NULL;

    char image[10];

    int ImageCounter = 0;





        do

        {
//Declare a buffer pointer to where we will store each chunk of memory to read
            BYTE buffer[512];


            fread(buffer, sizeof(BYTE) , 512, f); 




//check if it is a JPEG (header conditions).




            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
            {

                printf("entrou no if condicional\n");

//If it's the first JPEG, creates a file and writes it to it

                if (ImageCounter == 0)
                {

                    sprintf(image, "%03i.jpg", ImageCounter); 

//creates a name for the file that we will create, corresponding to the ith iteration



                    img = fopen(image, "w");

                    fwrite(buffer, sizeof(BYTE), 512, img);

                    ImageCounter++;
                }
//If it isn't the first JPEG, but since it's another header, we need to close the last one, and create a new JPEG

                if (ImageCounter > 0)
                {
                    fclose(img);

                    sprintf(image, "%03i.jpg", ImageCounter);

                    img = fopen(image,"w");

                    fwrite(buffer, sizeof(BYTE), 512, img);

                    printf("escreveu a imagem\n");


                    ImageCounter++;

                }
//this is supposed to store parts of the picture that are not the header

                img = fopen(image,"w");
                fwrite(buffer, sizeof(BYTE), 512, img);

            }


        }


        while(ImageCounter < 50);



    return 0;

}


1

The code is creating all the image files, but there's still a problem. If you do an ls -al *.jpg, you'll find that all the files are 512 bytes. Why didn't the data between signature blocks get written to the output files?

The code that writes the intermediate data is inside the if code block that handles the signature blocks, so it'll never process the non-signature data.

Be sure that once it handles that data, that it still discards the garbage data at the front of the card file. ;-)

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

  • Thank you, when i removed it from inside the if i got a segmentation error, but fixed it when i removed the ImageCounter and the write part from inside both if's placed outside. Very helpful! – Nivaldo Neto May 31 '20 at 17:00

You must log in to answer this question.

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