This is the code that I have written for PSET 4 recover.c

The question is that I have been provided with a card.raw which has jpg images which I have to recover by writing a recover.c file.

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <stdint.h>

typedef uint8_t BYTE;
#define BLOCKSIZE 512
int main(){

// open a file
FILE* file = fopen("card.raw","r");
    // if error in opening file --> then exit
  if (file == NULL){
      printf("Could not open file\n");
      return 2;

  BYTE buffer[BLOCKSIZE] = {};
  int i = 0;
  char fileName[8];

  // loop while end of file is not reached
  while (fread(&buffer,sizeof(buffer),1,file)!= 0){
  // if buffer is equal to the initials of jpg
      if (buffer[0]==0xFF && buffer[1] == 0XD8 && buffer [2] == 0xFF){
      // then open a file named 000.jpg;
        sprintf(fileName,"%03d.jpg", i);
      //  open a file with above said name
        FILE* fp = fopen(fileName,"a");
        do {

        } while (buffer[0]!=0xFF && buffer[1] != 0XD8 && buffer [2] != 0xFF);



I'm getting 50 files starting from 000.jpg to 049.jpg but many of my files are not complete. I m not able to understand why I'm not able to get complete files??


per the specs

The fourth byte, meanwhile, is either 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, or 0xef.

you are not ensuring the 4th byte is one of these.

also the continuation condition of your do..while loop doesn't seem to be logically doing what you're expecting it to do. aside from checking the value of the 4th byte, do you think the condition will get evaluated to true, if at least one of the sub conditions is false? in other words, will it get evaluated to true, if the first 4 bytes do not represent a JPG signature?

lastly, what happens if you never hit a JPG signature (e.g., no more images in card.raw to recover)?

  • Thanks Kareem... my logic was flawed .. i have wrote a completely new program and even got graded 1 in that.. But another problem has risen up now. my resize.c is passing all the tests but during grading it is getting 0. If you can then please guide. Jun 4 '16 at 12:11

You must log in to answer this question.

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