0

I was struggling with this code for three days. someone could help me? What should i fix? Please, if you are so kind, give me a tip, not the solution! I wrote a lots of comments to you can understand what I want to do.

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


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

typedef uint8_t BYTE;

//CHECK THERE ARE 2 ARGC AND A REAL FILE
if (argc != 2)
{
    printf("Usage: ./recover image\n");
    return 1;
}
//OPEN THE FILE
FILE *file = fopen(argv[1], "r");

if (file == NULL)
{
    printf("File not found\n");
    return 1;
}


//INFO
size_t sz;
BYTE buffer[512];
char namejpeg[8];
int counter = 0;
FILE *img;

//REPEAT UNTIL END OF CARD
while(!feof(file))
{
    //Read 512 bytes
    sz = fread(buffer, sizeof(BYTE), 512, file);
    //CHECK FIRST 4 BYTES
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 0xf0) == 0xe0))
    {
        //IF IT IS THE FIRST JPEG
        if (counter == 0)
        {
            //CREATE JPEGFILE
            sprintf(namejpeg, "%03i.jpeg", counter);
            //OPEN THE JPEGFILE IN A READ MODE
            img = fopen(namejpeg, "w");
            //WRITE THE JPEGFILE WITH THE BUFFER DATA
            fwrite(buffer, sizeof(BYTE), 512, img);
            counter++;
        }
        //IF IT IS NOT THE FIRST ONE
        else if (counter > 0)
        {
            //CREATE A JPEG WITH THE NUMBER THAT DESERVE
            sprintf(namejpeg, "%03i.jpeg", counter);
            //WRITE THE DATA BLOCK IN THE NEW JPEG
            fwrite(buffer, sizeof(BYTE), 512, img);
            counter++;
        }

    }
    //IF IT IS NOT THE HEAD OF DE JPEG BUT JUST HAVE FOUND A JPEG BEFORE. SO, IT SHOULD BE A BLOCK OF A JPEG.
    else
    {
        //IF NO JPEG HEAD, CONTINUE THE LOOP
        if(img == NULL)
        {
            continue;
        }
        //IF THERE WAS A JPEG BEFORE, WRITE DATA ON IT
        else if (counter > 0)
        {
            //escribir el bloque de 512bytes en ese JPEG
            fwrite(buffer, sizeof(BYTE), 512, img);
        }
    }
}
//CLOSE THE FILE I USED
fclose(file);
//SUCESS
return 0;
}

Thank you a lot! Santiago Correa from Argentina

0

There are several problems in the code, but as you asked, I'll just give you some pointers.

  1. What's the file name of an output file supposed to be? Look carefully.
  2. When the second signature block is found, what is done with the first output file?
  3. This one you haven't found yet. The last file isn't going to match. QUestion: What happens when the last data block is processed from the input file? What happens on the next read from the input file? ;-)

This should make you think and get you going, as requested!

Here's a programming tip. Look at this code:

    //IF NO JPEG HEAD, CONTINUE THE LOOP
    if(img == NULL)
    {
        continue;  
    }
    //IF THERE WAS A JPEG BEFORE, WRITE DATA ON IT
    else if (counter > 0)
    {
        //escribir el bloque de 512bytes en ese JPEG
        fwrite(buffer, sizeof(BYTE), 512, img);
    }

In this code, the if block is unnecessary. It can be completely deleted, leaving only the second if (without the else). The two if conditions basically ask if an output file has been opened or not. If a file IS opened, then do the fwrite. If it hasn't nothing has to be done, so the first if is needless code. You could shorten the code to just a single if 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 .