1

I ask for help. I do not find the error. I prompted and rewritten the code. Next is a new version, but the cs50 does not pass! The files have been resumed. performance control does not pass list of errors:

:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
    000.jpg not found
:( recovers middle images correctly
    001.jpg not found
:( recovers 015.jpg correctly
    015.jpg not found

My code:

#include <stdio.h>
#include <stdbool.h>
 
int main(int argc, char *argv[])
{
    //Make sure that I have one command line argument
    if (argc != 1)
    {
        fprintf(stderr, "Usage: ./recover\n ");
        return 0;
    }
 
    //Open the file entered into the command line
    FILE *file = fopen("card.raw", "r");
 
    //If the file does not exist, throw an error
    if (file == NULL)
    {
        fprintf(stderr, "Could not open card.raw\n ");
        return 1;
    }
 
    // Create outfile for picture
    FILE *img = NULL;
 
    // Create buffer and filename arrays
    unsigned char buffer[512];
    char filename[8];
 
    //Set counter for filename
    int counter = 0;
 
    //Set flag
    bool flag = false;
 
    //Read the file
    while (fread(buffer, 512, 1, file) == 1)
    {
        //Check if we are at the beginning of a JPEG
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //Close current JPEG, so we can start reading the next
            if (flag == true)
            {
                fclose(img);
            }
            //Condition for found JPEG
            else
            {
                flag = true;
            }
 
            sprintf(filename, "%03i.jpg", counter);
            img = fopen(filename, "w");
            counter++;
        }
 
        if (flag == true)
        {
            fwrite(&buffer, 512, 1, img);
        }
    }
 
    //Close all files
    fclose(file);
    fclose(img);
 
    //Success
    return 0;
}
  • I can not find any reason for a segfault, the only error seems to be that the condition if (argc != 1) should be if (argc != 2) – MARS Feb 21 '19 at 23:21
  • MARS, make that an answer. It is causing every test to fail. IF there is another problem, it deserves a new question. Also, check50 has been known to have a different problem, such as a seg fault, resulting from the same cause. – Cliff B Feb 22 '19 at 0:25
0

Following the recommendations of @Cliff B, I will answer your question. The program correctly recovers the images, it seems that a single error is the trigger of all the problems, as it appears in my comment, keep in mind that the program must accept two arguments, the name of the program, and the name of the file which contains the raw data, this means that argc = 2.

| improve this answer | |
  • In the modern task there is no need to transfer the filename as an argument. But your answer was helpful, I found some errors – Igor Bilinskyy Feb 22 '19 at 21:56
0

'fclose(fileIn)'-line after 'if (fileIn == NULL)' is not needed.
'return' alternately changed to 0 and 1 .

but checking (the test) still does not pass

now a list of errors :
:) recover.c exists. :) recover.c compiles. :) handles lack of forensic image :( recovers 000.jpg correctly 000.jpg not found :( recovers middle images correctly 001.jpg not found :( recovers 015.jpg correctly 015.jpg not found

All files have been restored and are in the folder !

| improve this answer | |
  • This "answer" looks like a new question. IF this is a new problem with the same pset, please post it as a new question. – Cliff B Feb 25 '19 at 15:26
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review – DinoCoderSaurus Apr 26 '19 at 2:25
0

I think that you should put "filename[8];" before the:

sprintf(filename, "%03i.jpg", counter); img = fopen(filename, "w"); counter++;

Because then when re-run a new image filename will be free to write. I think it is this because in my code it's the only big difference of your's code.

| improve this answer | |

You must log in to answer this question.

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