0

I am trying to work on Pset4 Recover. I am having trouble understanding exactly what to do. Any help explaning would be appreciated. I have started but I know it's not all right and I know it doesn't even compile properly like this. I am just not sure how to proceed.

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

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

{

    //Ensure user inputs value
    if (argc != 2)
    {
        printf("Correct Usage Name of File...\n");
        return 1;
    }

    //Open file
    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
    {
        printf("This card cannot be opened for reading.\n");
        return 1;
    }

    //Read 3 bytes from file
    unsigned char bytes[512];

    fread(bytes, 512, 1, file);

    if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff)
    {
        if (bytes[3] == 0xe0 || bytes[3] == 0xe1 || bytes[3] == 0xe2 || bytes[3] == 0xe3
            || bytes[3] == 0xe4 || bytes[3] == 0xe5 || bytes[3] == 0xe6 || bytes[3] == 0xe7
            || bytes[3] == 0xe8 || bytes[3] == 0xe9 || bytes[3] == 0xea || bytes[3] == 0xeb
            || bytes[3] == 0xec || bytes[3] == 0xed || bytes[3] == 0xee || bytes[3] == 0xef)
        {
            fprintf(file, "%03i.jpg", 0);
            FILE *img = fopen(file, "w");
        }
    }

}

I have updated it after watching the video, but I am still not yet there... any follow up suggestions...?

Thank you! The second video was very helpful. Now I have updated my code, but it still needs help...

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

int main(int argc, char *argv[])
{
    //Ensure user inputs value
    if (argc != 2)
    {
        printf("Correct Usage Name of File...\n");
        return 1;
    }

    //Open file
    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
    {
        printf("This card cannot be opened for reading.\n");
        return 1;
    }

    //Read 3 bytes from file
    unsigned char bytes[512];

    int filecount = 0;
    while (bytes != nul)
    {
        fread(bytes, 512, 1, file);

    if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff)
    {
        if (bytes[3] == 0xe0 || bytes[3] == 0xe1 || bytes[3] == 0xe2 || bytes[3] == 0xe3
            || bytes[3] == 0xe4 || bytes[3] == 0xe5 || bytes[3] == 0xe6 || bytes[3] == 0xe7
            || bytes[3] == 0xe8 || bytes[3] == 0xe9 || bytes[3] == 0xea || bytes[3] == 0xeb
            || bytes[3] == 0xec || bytes[3] == 0xed || bytes[3] == 0xee || bytes[3] == 0xef)
        {
            if (bytes == 512)
            {
                if (filecount = 0)
                {
                    sprintf(title, "%03i.jpg", 0);
                    FILE *img = fopen(img, "w");
                    fwrite(bytes, 512, 1, *img);
                }
                else
                {
                    fclose(file);
                    FILE *img = fopen(img, "w");
                    sprintf(title, "%03i.jpg", 0);
                    fwrite(bytes, 512, 1, *img);
                }
                int filecount += 1;

            }
        }
    }

    }

}


I worked on it a lot and watched the videos. I think I have tremendously improved my code, but I am still having some trouble. Anything you can point out that I need to change??

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

int main(int argc, char *argv[])
{
    //Ensure user inputs value
    if (argc != 2)
    {
        printf("Correct Usage Name of File...\n");
        return 1;
    }

    //Open file
    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
    {
        printf("This card cannot be opened for reading.\n");
        return 1;
    }

    //Read 3 bytes from file
    unsigned char bytes[512];

    int filecount = 0;
    bool going;
    {
        fread(bytes, 512, 1, file);
    //loop it so keeps going...
    while (going == true)
    {
        //check for the conditions of a jpeg..
        if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff)
        {
            if (bytes[3] == 0xe0 || bytes[3] == 0xe1 || bytes[3] == 0xe2 || bytes[3] == 0xe3
                || bytes[3] == 0xe4 || bytes[3] == 0xe5 || bytes[3] == 0xe6 || bytes[3] == 0xe7
                || bytes[3] == 0xe8 || bytes[3] == 0xe9 || bytes[3] == 0xea || bytes[3] == 0xeb
                || bytes[3] == 0xec || bytes[3] == 0xed || bytes[3] == 0xee || bytes[3] == 0xef)
            {
                //if conditions are met and you have found a jpeg
                {
                    //if this is the first jpeg you are finding, open a new file and copy the jpeg in
                    if (filecount == 0)
                    {
                        sprintf(*file, "%03i.jpg", filecount + 1);
                        FILE *file = fopen(file, "w");
                        fwrite(bytes, 512, 1, *file);
                    }
                    //if this is not the first jpeg, first close the previous jpeg file and then open a new one etc
                    else
                    {
                        fclose(file);
                        sprintf(*file, "%03i.jpg", filecount + 1);
                        FILE *file = fopen(file, "w");
                        //until end of jpeg
                        fwrite(bytes, 512, 1, *file);
                    }
                    //keeps track of what number jpeg you are up to
                    filecount += 1;

                }
            }
            //if not beg of new jpeg, but you are in middle of old jpeg
            else if (filecount != 0)
            {
                //keep writing until end of this jpeg
                fwrite(bytes, 512, 1, *file);
            }
            //you are not yet at a jpeg..
            else
            {
                //skip as you are not yet there..
            }
        }
    fclose(file);
    fclose(file);
    going = false;
    }

}


Thanks!

1

The following walkthrough videos should likely purvey sufficient guidance.

| improve this answer | |

You must log in to answer this question.

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