0

EDIT: Ok after trying for 3 hours after posting this I have finally managed to solve this pset. The problem wasn't actually a logical one but rather it was a problem with the type definition and the way I defined the buffer variable. If you are able to tell what's wrong with it do enlighten me as I am still unsure as to why the type definition in the code I posted is wrong.

I have been trying to solve pset4 recover for the past few days but no matter how I edit my code I am still unable to recover any pictures. I have added comments in my code in order to explain my own logic to myself, making sure it makes sense but I am still unable to solve it.

Hopefully a fresh perspective from someone else is what is needed to clear up the flaws in my code, your input on how I could edit my code is much appreciated!!

#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

//TODO:
//open memory card
//look for begining of new JPEG
//open new JPEG file(the size of a JPEG file is bigger than 512 bytes, each JPEG is one after another in memory)
//write 512 bytes until new JPEG is found
//stop at end of file

//pseudocode:
//open memory card
//repeat until end of card:
    //read 512 bytes into a buffer(temp variable)
    //if start of new JPEG(check the first 4 bytes)
        //if first JPEG
            //write the bytes into a new JPEG output file(###.jpg) ==> use sprintf()
        //else if its not the first JPEG
            //close file which you have been writing previous JPEG to
            //then open new file to write current JPEG into
    //else if its not the start of a new JPEG
        //if already found JPEG
            //continue writing JPEG into file
//close any remaining files

//defining new type
typedef uint8_t BYTE;

//number of bytes in a block
const BYTE BLOCK_SIZE = (BYTE) 512; //number of bytes of a block always remains constant at 512

int main(int argc, char *argv[])
{
    //if there are not exactly 2 command line arguments
    if (argc != 2)
    {
        printf("Usage: ./recover.c file name\n");
        return 1;
    }
    else
    {
        //if command line argument given is not a proper file name
        char *filename = "card.raw";
        if (strcmp(argv[1], filename) != 0)
        {
            printf("Key in a proper file name\n");
            return 2;
        }

        //remember file name
        char* infile = argv[1];

        //opening input file card.raw
        FILE *input = fopen(infile , "r");
        if (input == NULL)
        {
            printf("Could not open file.\n");
            return 3;
        }

        //creating output file pointer
        FILE *output;

        //creating buffer array to store information from the input file
        BYTE buffer[BLOCK_SIZE];
        //keeping track of how many JPEGs have been recovered
        int jpegnum = 0;
        //keeping track if new JPEG is found
        bool newjpegfound[50]; //50 elements in this array
        for (int i = 0 ; i < 50 ; i++)
        {
            newjpegfound[i] = false; //all values in this array are set to false
        }

        //iterating till the end of the file in 512 byte blocks
        while (fread(buffer, BLOCK_SIZE, 1, input))
        {
            //if start of new JPEG
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) //conditions for a  JPEG file
            {
                jpegnum++; //since new jpeg is found, +1 to the jpeg count
                newjpegfound[jpegnum - 1] = true;

                //if first JPEG
                if (jpegnum == 1)
                {
                    //write the bytes into a new JPEG output file(###.jpg) ==> use sprintf()
                    //creating the name of the file using sprintf
                    sprintf(filename, "%03i.jpg", 0); //since it is the first JPEG, file is 000.jpg
                    //creating new file with the name of ###.jpg
                    output = fopen(filename, "w"); 
                    //writing 512 bytes into output file from buffer array using fwrite
                    fwrite(buffer, BLOCK_SIZE, 1, output);
                }
                //else if not the first JPEG
                else
                {
                    //close file which you have been writing previous JPEG to
                    fclose(output);
                    //then open new file to write current JPEG into
                    sprintf(filename, "%03i.jpg", jpegnum);
                    output = fopen(filename , "w");
                    fwrite(buffer, BLOCK_SIZE, 1, output);
                }

            }
            //if not the start of new JPEG
            else
            {
                //if already found JPEG
                if (newjpegfound[jpegnum - 1] == true)
                {
                    //continue writing JPEG into output file
                    fwrite(buffer, BLOCK_SIZE, 1, output);
                }
            }
        }
        fclose(input);
    }
    return 0;
}

I added the pseudocode in Bryan's walkthrough in order to refer to it more easily. Thank you!
0

You are missing the header file in which these data types are defined:

#include <stdint.h>

You must log in to answer this question.

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