0

I get 50 images, all load correctly. I assume it's something to do with the naming of the files or how I'm creating the images because I get all the files and they all open.

Any help would be appreciated.

~/workspace/pset4/jpg/ $ check50 2015.fall.pset4.recover recover.c
:) recover.c exists
:) recover.c compiles
:( recovers 000.jpg correctly
:( recovers middle files correctly
:( recovers last file correctly
\ expected 015.jpg to exist

https://sandbox.cs50.net/checks/d58be761e9f44881af112fb304fefd05

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

//init structs
typedef uint8_t  BYTE;
typedef struct
{   
BYTE  first;
BYTE  second;
BYTE  third;
BYTE buffer[509];
} __attribute__((__packed__))
JPGFILE;

typedef struct
{   
BYTE  first;
} __attribute__((__packed__))
SINGLE;

typedef struct
{
BYTE  first;
BYTE  second;
BYTE  third;
} __attribute__((__packed__))
JPGID;




int main(void)
{
// open I/O files 
FILE* file = fopen("card.raw", "r");
FILE* outptr;
    if (file == NULL)
{
    printf("Could not open.\n");
    return 1;
}

//init variables
char name[8];
int img = 000;
int curser;
JPGFILE bf;
JPGID triple;
SINGLE single;
fseek(file, 0, SEEK_END);
int end = ftell (file);
fseek(file, 0, SEEK_SET);

do
{
    // read JPGID from infile
    fread(&single, sizeof(SINGLE), 1, file);
    curser = ftell (file);
    if (single.first == 0xff)
    {
        fseek(file, -1, SEEK_CUR);
        fread(&triple, sizeof(JPGID), 1, file);


        //enter into first jpg
        curser = ftell (file);
        if (triple.first == 0xff && triple.second == 0xd8 && triple.third == 0xff)
        {
            fseek(file, -3, SEEK_CUR);
            curser = ftell (file);


            do
            {
                curser = ftell (file);
                fread(&bf, sizeof(JPGFILE), 1, file);

                //create file 
                if (bf.first == 0xff && bf.second == 0xd8 && bf.third == 0xff)
                {
                            sprintf(name,"%03i.jpg",img);
                            outptr = fopen(name, "w");
                            fwrite(&bf, sizeof(JPGFILE), 1, outptr);
                            img++;
                }

                else
                {   //continue writing file
                    fwrite(&bf, sizeof(JPGFILE), 1, outptr);
                }
            } while (curser < end); //finish at the end of the drive.
        }

    }

} while (curser < end);

// close files
fclose(file);
fclose(outptr);


}
1
  • so part of the problem is that I was scanning for the first 0xFF value, but I had to scan in full 512 bits from the start. Nov 3 '16 at 22:12
0

The entire problem was that I wasn't searching in 512 chunks the whole time.

You must log in to answer this question.

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