Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- int main(int argc, char *argv[])
- {
- // first check to see if what user entered made sense:
- if (argc != 2)
- {
- fprintf(stderr, "Usage: copy infile outfile \n");
- return 1;
- }
- // remember filenames
- char *infile = argv[1]; // for my own edification: store argv[1] in a char array whose address we shall call "infile"
- // open input file
- 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.
- if (inptr == NULL)
- {
- fprintf(stderr, "Could not open %s.\n", infile);
- return 2;
- }
- int counter = 0; // will keep track of how many jpgs we've found so far, will use in file names
- unsigned char current_block[512]; // since each unsigned char is 1 byte and we want to read 512 bytes at a time
- char current_out_name[8]; // xxx.jpg is seven chars, plus \0 makes eight total
- FILE *img = NULL;
- while (1) // this loop finds and writes the first 512B block of our first jpg
- {
- fread(current_block, 512, 1, inptr); // read 1 512B block starting at address inptr and put into current_block called "current_block"
- 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)
- {
- sprintf(current_out_name, "%03i.jpg", counter); // make a string that will eventually be the name of current output file
- img = fopen(current_out_name, "w"); // give ourselves permission to write into file for our first image
- 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
- counter++;
- break;
- }
- }
- while (fread(current_block, 512, 1, inptr) == 1) // while there remains at least one whole 512B block left in the file
- {
- if (current_block[0] == 0xff && current_block[1] == 0xd8 && current_block[2] == 0xff && (current_block[3] & 0xf0) == 0xe0) // from Zamyla
- {
- // close the old file and write the next 512 bytes into the new file (we've already incremented counter)
- fclose(img);
- sprintf(current_out_name, "%03i.jpg \n", counter); // make a string that will eventually be the name of new output file
- img = fopen(current_out_name, "w"); // give ourselves permission to write into file for next image
- 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
- counter++;
- }
- else
- {
- fwrite(current_block, 512, 1, img);
- }
- }
- fclose(img);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement