0

I'm not good a programming but I'm really trying so I've been stuck for days in the same problem and any advice could be helpful Here's my code:

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

#define BLOCK 512
typedef uint8_t BYTE;


int main (int argc, char* argv[])
{
 //Usage
 if (argc != 2)
 {
   printf("Usage: ./recover image");
    return 1;
 }

 //open the file
 FILE *fcard = fopen(argv[1], " r");
   //if we can not open
   if (fcard == NULL)
   {
       printf("Unable to open this file");
       return 1;
   }

 BYTE buffer[BLOCK];
 int counter = 0;
 char filenames[8];
 FILE *nwFile = NULL;
 bool jHeader = buffer[0] == 0xff && buffer[1]==0xd8 && buffer[2]== 0xff && (buffer[3] & 0xf0)== 0xe0;

 //fread
 while (fread(buffer, sizeof(BYTE), BLOCK , fcard) || feof(fcard)==0)
 {

  //start of new JPEG
    if(jHeader && nwFile != NULL)
    {
           fclose(nwFile);
    }

  if(jHeader)
  {    //in case this is not the first time
     //start writing 000.jpeg
     sprintf(filenames,  "%03i.jpg", counter);
     nwFile = fopen(filenames, "w");
     counter++;
  }


   if(nwFile != NULL)
   {
       fwrite(buffer, sizeof(buffer), 1, nwFile);
   }


  }
 //close all the files
 fclose(nwFile);

 fclose(fcard);

 return 0;

 }
0

Have a look at your definition for jHeader. It gets evaluated once and assigned before any data is read in the loop. Remember that when you reference it within the loop the stored value will be used. The expression won't be re-evaluated again unless you do so directly.

You must log in to answer this question.

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