0
#include <stdio.h>
#include <stdint.h>
#include <cs50.h>

typedef uint8_t  BYTE;

int main (int argc , char* argv[] )
{
    //ensure correct usage in terminal
    if (argc!=2)
    {fprintf(stderr,"usage:./recover image");
    return 1;}

    //open file for reading
    FILE *file = fopen(argv[1],"r");
    if (file==NULL)
    {return 2;}

    //create temorary storage for reading
    BYTE str[512];
    char name[50];

    //used for breaking the loops
    int x=0,y=0,n=0;

    while (!EOF)
    {//one loop cycle gives one jpeg

        //naming and opening a jpg for writing
         if(n<10)
         { //convert the n int to a string for naming
          sprintf(name,"%03d.jpg",n);}

          else if(n>=10)
         { //convert the n int to a string for naming
          sprintf(name,"%03d.jpg",n);}

          n=n+1;

          FILE *photo = fopen(name,"w");
          if(photo == NULL)
          {eprintf("name which is the .jpg format file was not created");}

        //getting to the start of first jpg and writing first block into it
       while(y)
        {
         fread(str,1,512,file);
         if(str[0]==0xff && str[1]==0xd8 && str[2]==0xff && (str[3] & 0xf0) == 0xe0)
          {fwrite(str,1,512,photo);
           y=1;}
          if(EOF)
          {y=1;}
        }

     //continuing writing in same jpeg
      do{
           if(EOF)
          {x=1;
            fclose(photo);
          }

        fread(str,1,512,file);

        //if another jpeg starts closing first and getting pointer correct
        if(str[0]==0xff && str[1]==0xd8 && str[2]==0xff && (str[3] & 0xf0) == 0xe0)
        {fseek(file,-512,SEEK_CUR);
         fclose(photo);
         x=1;}

        else
        {fwrite(str,1,512,photo);}

        }

      while(x==0);

    }
    fclose(file);
}
1

!EOF is the logical negation of EOF. The constant (constants in C are written all caps) EOF has a negative, and non-zero value. Therefore, !EOF is 0, or false. You never enter the loop.

Maybe you meant feof(file), but that has its own limitations. For example, it signals end of file only after an unsuccessful read, so that it would have to be called directly after attempting to read.

The function fread returns the number of blocks read (you specify block size and maximum number of blocks to read from file), so you might want to check that.

This code is hard to read. Please give your variables longer names that actually describe the variable and help understand its use and how it differs from other variables. One-letter variables should if at all be limited to very short sequences of code. And file also isn't that great if you are dealing with both input and output files, a card dump and individual JPEG files. Try to write your code in a way you could explain it to another person. Which is a good idea anyway, even if you explain to a rubber duck.

Also, you have different places where you fread, fwrite, check for JPEG header, check for EOF (the wrong way). Maybe you can reduce those, and also avoid re-reading a sector.

You must log in to answer this question.

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