Advertisement
bencundiff

Recover - round 2

Apr 20th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     // ensure proper usage
  7.     if (argc != 2)
  8.     {
  9.         fprintf(stderr, "Usage: recover imagefile\n");
  10.         return 1;
  11.     }
  12.  
  13.     // remember filenames
  14.     char *imagefile = argv[1];
  15.     bool control = true;
  16.     //bool jpegfound = false;
  17.     int imagenumber = 0;
  18.     char filenamestring[50] = "000.jpg";
  19.     bool writingjpg = false;
  20.     //int secondimagecount=0;
  21.     //char buffer[4];
  22.  
  23.     //we only open this guy up once.
  24.     FILE *fileinputpointer = fopen(imagefile, "r");
  25.     //FILE *outptr = fopen(filenamestring, "w");//making an assumption 1 or more jpeg headers will be found. In this non-real world example, this assumption is justified.
  26.     FILE *outptr;
  27.     if (fileinputpointer == NULL)
  28.     {
  29.         fprintf(stderr, "Could not open image file %s.\n", imagefile);
  30.         return 2;
  31.     }
  32.     while(control)
  33.     {
  34.        unsigned char buffer[512];//re-initialize as blank
  35.  
  36.         int bytesreturned = fread(&buffer,512,1,fileinputpointer);
  37.         //if(new header found and imagenumber > 0)
  38.         if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
  39.         //if(buffer[0] == (unsigned char)0xff && buffer[1] == (unsigned char)0xd8 && buffer[2] == (unsigned char)0xff && (buffer[3] & (unsigned char)0xf0) == (unsigned char)0xe0)
  40.         {
  41.             //Update the filename. Make a new pointer with new filename.
  42.             sprintf(filenamestring,"%03d.jpg", imagenumber);
  43.             printf("this is filenamestring:\n%s\n",filenamestring);
  44.             if(writingjpg)//imagenumber > 0
  45.             {
  46.              fclose(outptr);
  47.              printf("closing file output pointer\n");
  48.              outptr = fopen(filenamestring, "w"); //idk when to initialize
  49.              imagenumber++;
  50.             }
  51.             else
  52.             {
  53.                 writingjpg = true;
  54.                 //now is when we open new file open pointer
  55.                outptr = fopen(filenamestring, "w"); //idk when to initialize
  56.                 fwrite(&buffer,512,1,outptr);//read from buffer,write to FILE*
  57.             }
  58.  
  59.  
  60.             if (outptr == NULL)
  61.             {
  62.                 fclose(fileinputpointer);
  63.                 fprintf(stderr, "Could not create %s.\n", filenamestring);
  64.                 return 3;
  65.             }
  66.  
  67.         }//end of "if header found"
  68.     else//if header was not found
  69.     {
  70.         if(writingjpg)//imagenumber > 0
  71.         {
  72.             fwrite(&buffer,512,1,outptr);//read from buffer,write to FILE*
  73.  
  74.         }
  75.     }
  76.         //we write the things we just read to outpointer to the file--
  77.  
  78.         //because we hit EOF, it's time to close the file.
  79.         if(bytesreturned != 1 )//1 chunk of 512 bytes
  80.         {
  81.              //we hit eof
  82.              control = false;
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement