0

I have completed the task and can view the .jpg images but when I run check50 I get the following error message:

:) recover.c exists.
:( recover.c compiles.
expected exit code 0, not 1
...etc.

When I run echo $? in the terminal I get a value of zero so this doesn't appear to be referring to main's return value. Any ideas?

/**
 * Recovers jpeg from raw file.
 *
 * */

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

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage:1 file names as arg
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover infile\n");
        return 1;
    }

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

    // create file pointer to read input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)  //check that it is pointing somewhere
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    fseek(inptr, 0L, SEEK_END); //go to end of file
    int size = ftell(inptr);        //store length of file in bytes
    rewind(inptr);              //rewind to start of file. equivalent to: fseek(inptr, 0L, SEEK_SET);
    int num_blocks = size/512;      //total number of blocks. Note that the final E0F block (which might be less than 512 bytes)
                                    //will be lost but this is good

    uint8_t* buffer = malloc( 512 * sizeof(uint8_t)); //get buffer pointer to series of byte long ints
    if (buffer == NULL)  //check that it is pointing somewhere
    {
        fprintf(stderr, "Could not allocate memory.\n");
        return -1;
    }

    int jpg_count = 0;


    for (int block = 0; block < num_blocks; block++)
    {

        fread(buffer, 1, 512, inptr); // store a block in the buffer


        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] & 0xf0) == 0xe0)
        {
            char filename[10];   //create filename
            sprintf(filename, "%03i.jpg", jpg_count);

            jpg_count++;
            FILE *img = fopen(filename, "w"); //create file pointer to append to output file
            fwrite(buffer, 1, 512, img);   //copy Block to img
            fclose(img);

        }
        else
        {
            if (jpg_count > 0)
            {
                char filename[10];   //create filename
                sprintf(filename, "%03i.jpg", jpg_count -1);

                FILE *img = fopen(filename, "a");
                fwrite(buffer, 1, 512, img);
                fclose(img);
            }
        }
    }
    free(buffer);


    // close infile
    fclose(inptr);

    //if success
    return 0;
}

1 Answer 1

1

There's no bmp.h file in the path, so compilation fails. Remove that part, it's needed for resize, not recover.

1
  • Thank you so much! I've been looking at this for as long as as it took me to do the first 4 psets.
    – jules654
    Oct 25, 2017 at 9:39

You must log in to answer this question.

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