Advertisement
Orpuwupetup

formerrecover.c

Oct 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 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.  
  29.     int buffer[512];
  30.     int x;
  31.     int jpgcnt = 0;
  32.     char jpgname[7];
  33.     FILE *img;
  34.     // actual images recovering
  35.  
  36.     while (true)
  37.     {
  38.         //write next 512 bytes of inptf to buffer
  39.         fread(buffer, sizeof(int) * 512, 1, inptf);
  40.  
  41.         //go back 512 bytes and check them one more time with fseek to find if this 'box' is last in file
  42.         fseek(inptf, -512, SEEK_CUR);
  43.         x = fread(buffer, 1, 512, inptf);
  44.  
  45.         if (x == 512)
  46.         {
  47.             //check if it is beggining of the jpg
  48.             if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
  49.             {
  50.  
  51.                 //if first encountered photo on card
  52.                 if (jpgcnt == 0)
  53.                 {
  54.                     //create new jpgfile name and open new file with this name
  55.                     sprintf(jpgname, "%03i.jpg", jpgcnt);
  56.                     img = fopen(jpgname, "w");
  57.                     if (img == NULL)
  58.                     {
  59.                         return 3;
  60.                     }
  61.                     //write chunk of photo to new file
  62.                     fwrite(buffer, 512, 1, img);
  63.                     jpgcnt++;
  64.                 }
  65.                 // when there were photos encountered on card before
  66.                 if(jpgcnt > 0)
  67.                 {
  68.                     //first close former image file, then create name for new one, open file with this name an write chunk of photo
  69.                     fclose(img);
  70.                     sprintf(jpgname, "%03i.jpg", jpgcnt);
  71.                     img = fopen(jpgname, "w");
  72.                     if (img == NULL)
  73.                     {
  74.                         return 3;
  75.                     }
  76.                     fwrite(buffer, 512, 1, img);
  77.                     jpgcnt++;
  78.                 }
  79.             }
  80.             //if we dont encounter new photo, and have one img file already opened, write other chunks of photo into it
  81.             else if (jpgcnt > 0)
  82.             {
  83.                 fwrite(buffer, 512, 1, img);
  84.             }
  85.             }
  86.         // if last "box" of file, write bytes to output file, then break the loop
  87.         else if (jpgcnt > 0)
  88.         {
  89.             fwrite(buffer, 512, 1, img);
  90.             fclose(img);
  91.             break;
  92.         }
  93.  
  94.  
  95.  
  96.     }
  97.  
  98. // close infile at the and of the program
  99.     fclose(inptf);
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement