SHARE
TWEET

Untitled

a guest Jun 19th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     // first check to see if what user entered made sense:
  8.     if (argc != 2)
  9.     {
  10.         fprintf(stderr, "Usage: copy infile outfile \n");
  11.         return 1;
  12.     }
  13.    
  14.     // remember filenames
  15.     char *infile = argv[1];    // for my own edification: store argv[1] in a char array whose address we shall call "infile"
  16.    
  17.     // open input file
  18.     FILE *inptr = fopen(infile, "r");  // for my own edification: fopen returns the address of the input file (NOT of its name). We declare that to be the address (called "inptr") of a FILE.
  19.     if (inptr == NULL)
  20.     {
  21.         fprintf(stderr, "Could not open %s.\n", infile);
  22.         return 2;
  23.     }
  24.    
  25.     int counter = 0; // will keep track of how many jpgs we've found so far, will use in file names
  26.    
  27.     unsigned char current_block[512];  // since each unsigned char is 1 byte and we want to read 512 bytes at a time
  28.     char current_out_name[8]; // xxx.jpg is seven chars, plus \0 makes eight total
  29.     FILE *img = NULL;
  30.    
  31.    
  32.     while (1) // this loop finds and writes the first 512B block of our first jpg
  33.     {
  34.         fread(current_block, 512, 1, inptr); // read 1 512B block starting at address inptr and put into current_block called "current_block"
  35.        
  36.         if (current_block[0] == 0xff && current_block[1] == 0xd8 && current_block[2] == 0xff && (current_block[3] & 0xf0) == 0xe0) // from Zamyla (tells us if we're at start of jpg)
  37.         {
  38.             sprintf(current_out_name, "%03i.jpg", counter); // make a string that will eventually be the name of current output file
  39.             img = fopen(current_out_name, "w"); // give ourselves permission to write into file for our first image
  40.             fwrite(current_block, 512, 1, img); // write 1 512B block taken from inptr onto current_out_name, the output file w/ which we're working
  41.             counter++;
  42.             break;
  43.         }
  44.     }
  45.    
  46.     while (fread(current_block, 512, 1, inptr) == 1) // while there remains at least one whole 512B block left in the file
  47.     {
  48.         if (current_block[0] == 0xff && current_block[1] == 0xd8 && current_block[2] == 0xff && (current_block[3] & 0xf0) == 0xe0) // from Zamyla
  49.         {
  50.             // close the old file and write the next 512 bytes into the new file (we've already incremented counter)
  51.             fclose(img);
  52.             sprintf(current_out_name, "%03i.jpg \n", counter); // make a string that will eventually be the name of new output file
  53.             img = fopen(current_out_name, "w"); // give ourselves permission to write into file for next image
  54.             fwrite(current_block, 512, 1, img); // write 1 512B block taken from inptr onto current_out_name, the output file w/ which we're working
  55.             counter++;
  56.         }
  57.         else
  58.         {
  59.             fwrite(current_block, 512, 1, img);
  60.         }
  61.        
  62.     }
  63.     fclose(img);
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top