0

Hey so I have written some code, needless to say I am a bit confused, I have pasted it below. It is for pset4 recover and so I want to open the files that are contained in card.raw, however despite using eprintf and fopen() seemingly working, no files appear in the directory. Now this may either be that my code is hopelessly wrong, or that I need to use an if statement for the first 4 bytes of a JPEG, before the files will open. However so far, I have set 2 command-line arguments, called the open function, and then tried to use sprintf to systematically name each JPEG. Any help at all would be much appreciated! x

#include <stdio.h>

int main(int argc, char* argv[])
{
// ensure only 1 command line argument
if (argc != 2)
{
    fprintf(stderr, "Usage: ./recover image\n");

    return 1;
}


// open memory card file
FILE *file = fopen(argv[1], "r");


// check the fill isnt empty
if (file == NULL)
{
    fclose(file);
    fprintf(stderr, "fopen() error, retry\n");
    return 1;
}


// confirm fopen() worked
eprintf("File open sucsessfully\n");


for (int i = 0; i < 51; i++)
{
char file_name[50];

sprintf(file_name, "%03d.JPEG", i);
}

char file_name = '\0';
fopen(&file_name, "r");

}

1
  • Can anyone help? – stabilo Jul 8 '17 at 17:39
1

Remember that you need to make a new file to write the images two. It looks like you successfully open card.raw, but all you do is name it in your for loop. You probably want to first read from your file to some sort of buffer that is 512 bytes long. Why? Because you know that each jpeg is sectioned into blocks each using the FAT system explained in the beginning of the pset under the background header, so it makes sense to recover the images 512 bytes at a time, but make sure you have a condition that checks that you have not reached the end lest you suffer a segmentation fault (I presume).

You also have to check if each 512 byte block is in fact the beginning of a jpeg by checking the special jpeg signatures. Most if not all of this Zamyla goes through in the walk through, so make sure you have watched that and understand each step. There are some good questions asked by others as well, here are a few you might find helpful:

pset 4 recover - Bitwise operator

Recover pset4: help with pseudocode logic

Detecting if we already found a jpg

You can always ask more questions here, or on other sites dedicated to cs50 such as the cs50 reddit or cs50 facebook group. Feel free to clarify anything in the comments below. Never give up!

1
  • Hey, I have advanced the code a long way since I posted this, however I am at a dead end and have been struggling for a while at this spot. I was hopping that you could look at my new question and use your master gaze! x cs50.stackexchange.com/questions/25939/… – stabilo Jul 11 '17 at 22:37

You must log in to answer this question.

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