1

I have problem with pset4/recover. I recover all images and can see last image when I click on it but when I run check50 it returns with error: :( recovers 049.jpg correctly recovered image does not match. My code is here:

// Ivan Stimac
// 3 October 2018
// CS50 Pset 4/ recover
// input: read in a card.raw file
// output: 50 jpg images


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




int main(int argc, char *argv[])
{
    // ensure correct usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage --> recover card.raw\n");
        return 1;
    }

    // try to open a file for reading
    char *infile = argv[1];
    FILE *fp = fopen(infile, "r");
    if (fp == NULL)
    {
        fprintf(stderr, "Can't open file: %s\n", infile);
        return 2;
    }
    // create array of file pointers for 50 images,variable for file name and counter
    FILE *fi[50];
    char *fileName[50] = {"000.jpg", "001.jpg", "002.jpg", "003.jpg", "004.jpg", "005.jpg", "006.jpg", "007.jpg", "008.jpg",
                          "009.jpg", "010.jpg", "011.jpg", "012.jpg", "013.jpg", "014.jpg", "015.jpg", "016.jpg", "017.jpg",
                          "018.jpg", "019.jpg", "020.jpg", "021.jpg", "022.jpg", "023.jpg", "024.jpg", "025.jpg", "026.jpg",
                          "027.jpg", "028.jpg", "029.jpg", "030.jpg", "031.jpg", "032.jpg", "033.jpg", "034.jpg", "035.jpg",
                          "036.jpg", "037.jpg", "038.jpg", "039.jpg", "040.jpg", "041.jpg", "042.jpg", "043.jpg", "044.jpg",
                          "045.jpg", "046.jpg", "047.jpg", "048.jpg", "049.jpg"
                         };
    int counter = -1;

    // falg to see if next file needs to be written
    char flag = 0;


    // read a 512B block process it and
    char magicNum[3] = {0xff, 0xd8, 0xff};

    while (1)
    {
        char *buffer = malloc(sizeof(char) * 512);
        memset(buffer,0,512);
        fread(buffer, sizeof(char), 512, fp);
        if (buffer[0] == magicNum[0])
        {

            // if it is magic num
            if (buffer[1] == magicNum[1] && buffer[2] == magicNum[2])
            {
                counter += 1;

                //close last file and open next
                if (counter > 0)
                {
                    fclose(fi[counter - 1]);
                }

                fi[counter] = fopen(fileName[counter], "w");
                if (fi[counter] == NULL)
                {
                    fprintf(stderr, "Can't open file: %s\n", infile);
                    return 2;
                }

                // set flag
                flag = 1;
            }


        }


        if (flag)
        {
            fwrite(buffer, sizeof(char), 512, fi[counter]);
        }

        free(buffer);
        // if end of file exit
        if (feof(fp))
        {
            // close infile
            fclose(fp);
            // close last file
            fclose(fi[counter]);
            return 0;
        }
    }


    // close infile
    fclose(fp);

}

Thanks for help, Ivan

2

You did not check the result of fread().

Before using the result, you should check for errors.

And if there are no errors, you should check how many bytes were read, before writing 512 bytes to the output file ...


And to quote Cliff B:

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

| improve this answer | |
  • Thanks, this was the solution. – Ivan Stimac Oct 3 '18 at 13:09

You must log in to answer this question.

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