Advertisement
Orpuwupetup

recover.c

Oct 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4.  
  5.  
  6. int main (int argc, char* argv[])
  7. {
  8.     //Check proper usage
  9.     if (argc != 2)
  10.     {
  11.         fprintf(stderr, "Usage: ./recover infile\n");
  12.         return 1;
  13.     }
  14.  
  15.     // Remember infile name
  16.    char *infile = argv[1];
  17.  
  18.    // create pointer in reading mode leading to infile
  19.    FILE *inptf = fopen(infile, "r");
  20.  
  21.    //check if correct infile, not leading to NULL
  22.     if (inptf == NULL)
  23.     {
  24.         fprintf(stderr, "Can't read infile\n");
  25.         return 2;
  26.     }
  27.     // declare variables that will be used inside of loops
  28.     int buffer[512];
  29.  
  30.     int x = 0;
  31.     int jpgcnt = 0;
  32.     char jpgname[7];
  33.     FILE *img;
  34.     // actual images recovering
  35.  
  36.     do
  37.     {
  38.          // read infile next 512 bytes to buffer
  39.         fread(buffer, 512 * sizeof(int), 1, inptf);
  40.  
  41.         fseek(inptf, -512, SEEK_CUR);
  42.         x = fread(buffer, 1, 512, inptf);
  43.  
  44.             if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
  45.             {
  46.  
  47.                 //if first encountered photo on card
  48.                 if (jpgcnt == 0)
  49.                 {
  50.                     //create new jpgfile name and open new file with this name
  51.                     sprintf(jpgname, "%03i.jpg", jpgcnt);
  52.                     img = fopen(jpgname, "w");
  53.                     if (img == NULL)
  54.                     {
  55.                         return 3;
  56.                     }
  57.                     //write chunk of photo to new file
  58.                     fwrite(buffer, 512, 1, img);
  59.                     jpgcnt++;
  60.                 }
  61.                 // when there were photos encountered on card before
  62.                 if(jpgcnt > 0)
  63.                 {
  64.                     //first close former image file, then create name for new one, open file with this name an write chunk of photo
  65.                     fclose(img);
  66.                     sprintf(jpgname, "%03i.jpg", jpgcnt);
  67.                     img = fopen(jpgname, "w");
  68.                     if (img == NULL)
  69.                     {
  70.                         return 3;
  71.                     }
  72.                     fwrite(buffer, 512, 1, img);
  73.                     jpgcnt++;
  74.                 }
  75.             }
  76.             //if we dont encounter new photo, and have one img file already opened, write other chunks of photo into it
  77.             else if (jpgcnt > 0)
  78.             {
  79.                 fwrite(buffer, 512, 1, img);
  80.             }
  81.     }
  82.             // check if this was last 'box' in file
  83. while (x == 512);
  84.  
  85. // write last bytes to output file, then close output file
  86.             fwrite(buffer, 512, 1, img);
  87.             fclose(img);
  88.  
  89. // close infile at the and of the program
  90.     fclose(inptf);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement