1

When try to submit, CS50 pset4 recover.c then got an error

:) recover.c exists. :) recover.c compiles. :) handles lack of forensic image :) recovers 000.jpg correctly :) recovers middle images correctly :( recovers 049.jpg correctly recovered image does not match

My code is

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

#define FAT 512

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: name of forensic image from which to recover JPEGs");
        return 1;
    }
    FILE *file = fopen(argv[1], "r");
    if (!file)
    {
        fprintf(stderr, "Could not open %s.\n", argv[1]);
        fclose(file);
        return 2;
    }
    char *filename = malloc(50 * sizeof(int));

    BYTE buffer[FAT];
    if (!filename)
    {
        return 3;
    }
    FILE *curImg;
    int count = 0;
    while (!feof(file))
    {
        fread(buffer, sizeof(buffer), 1, file);
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            if (count > 0)
                fclose(curImg);

            sprintf(filename, "%03i.jpg", count);
            curImg = fopen(filename, "w");
            if (curImg)
                fwrite(buffer, sizeof(buffer), 1, curImg);
            count++;
        }
        else
        {
            if (curImg)
                fwrite(buffer, sizeof(buffer), 1, curImg);
        }
    }
    free(filename);
    fclose(file);
    return 0;
}

I didn't understood, what's the problem. Anyone can help me?

Md. Abdul Alim
  • 707
  • 1
  • 6
  • 19
  • Can you show the recovered image and the image which should have been recovered? Can you show the input? What from do you recover? Distortion? Encryption? Packing? Please provide much more info, assume that not everybody here knows the cs50 assignments. – Yunnosch Nov 01 '20 at 06:50
  • recovered image success. It has been solved. Thanks – Md. Abdul Alim Nov 01 '20 at 06:52
  • You should take a look at [why `while (!feof(file))`is wrong`](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Gerhardh Nov 01 '20 at 10:52
  • Using any input/output function without checking the return value is a no-go! – Gerhardh Nov 01 '20 at 10:54
  • 1
    Does this answer your question? [CS50 recover segmentation fault Oct 2020](https://stackoverflow.com/questions/64368232/cs50-recover-segmentation-fault-oct-2020) – Enis Arik Nov 01 '20 at 18:08
  • @earik87, I've no segmentation fault. The problem was founded by me i.e. overwrite last file for one times. and it was solved, answer in below. – Md. Abdul Alim Nov 03 '20 at 00:18

1 Answers1

0

It was happen for 'end of file indicator'. That means after writing file, something write in last file for a time. I just add a condition in

if (curImg)
     fwrite(buffer, sizeof(buffer), 1, curImg);

replace by

if (curImg && !feof(file))
     fwrite(buffer, sizeof(buffer), 1, curImg);
Md. Abdul Alim
  • 707
  • 1
  • 6
  • 19