0

I am having trouble getting recover to function in any capacity like it is supposed to. Any advice as to what I should fix?

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

typedef uint8_t  BYTE;

int main(void)
{
    // Open input file
    FILE* file = fopen("card.raw", "r");

    //define variables
    BYTE buffer[512];
    int i = 0;
    char title[8];
    FILE* img = NULL;

    //read entire memory card
    while(fread(buffer, sizeof(BYTE), 512, file) == 512)
    {

        //if bytes match the jpeg sequence
        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] == 0xe0 ||buffer[3] == 0xe1))
        {
            if (i > 0)
            {
                //closes open jpeg file
                fclose(img);
            }

            //names file correctly

            sprintf(title, "%03d.jpg\n", i);
            i++;

            //opens a jpeg file of the correct name
            img = fopen(title, "a");


            //writes jpeg to file
            fwrite(buffer, sizeof(BYTE), 512, img);
        }
        else
        {
            if (i > 0)
            {
                //continues writing jpeg to file until a new jpeg is found
                fwrite(buffer, sizeof(BYTE), 512, img); 
            }
        }
    }

    // the end
    fclose(img);
    fclose(file);

    return 0;

}

1 Answer 1

1

Have you looked at the output file names by doing ls -al ? You would see that they are named 000.jpg?? and 001.jpg??. This is a problem. Now look at the following:

        sprintf(title, "%03d.jpg\n", i);

Why is there a line feed in the file name?

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

You must log in to answer this question.

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