0

Currently getting the following errors when working on pset4 for the class CS50. Not sure why my exit code is none? Please let me know if you guys know what the issue is. Thank You.

:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
    expected exit code 0, not None
:( recovers middle images correctly
    expected exit code 0, not None
:( recovers 049.jpg correctly
    expected exit code 0, not None

Code:

#include <cs50.h>       
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define BLOCKSIZE 512
#define Filesize 8
typedef uint8_t BYTE;


int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage ./recover image\n");
        return 1;
    }
    
    int counter = -1;
    BYTE buffer[BLOCKSIZE]; //creates a buffer with size BLOCKSIZE taking up memory of 8 bits 
    char filename[8]; // creates an array named filename that can hold up to 8 bits, each char is a bit xxx.jpg\0
    FILE *inputFp = fopen(argv[1], "r");
     if (inputFp == NULL)
    {
        printf("Could not open card.raw.\n");
        return 2;
    }
    FILE *outputFp = NULL;
    
    bool foundjpg = 0;
    
    while(fread(buffer, 512, 1, inputFp) == 1 )  // fread(Ptr to where you store it to, size of the elements you read, number of elements, where you read from)
    {
       if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xe0) == 0xe0)
       {
           
            if (foundjpg == 1)
            {
                fclose(outputFp);
            }
            else
            {
         foundjpg = 1;
         counter = counter +1;
         sprintf(filename, "%03d.jpg", counter);
         outputFp = fopen(filename, "a");
         fwrite(buffer, BLOCKSIZE, 1, outputFp);
            }
           
        
        }
        
    }
    
    fclose(inputFp);
    fclose(outputFp);
    
     return 0;

}
0

The tests fail because the code is only creating one file, 000.jpg. The program is terminating when it tries to close the first file and open a second one, so no exit code is being generated.

Hint: should there be an else statement???

There are more problems, but you should have a chance to identify them. Hint. How big are the output files?

BTW, how much did you test the program and look at the results before running check50?

If this answers your questions, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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