Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- // jpegs always have a header 0xFF 0xD8 0xFF 0xEn where n can equal 0 through F
- typedef uint8_t BYTE;
- int main(int argc, char *argv[])
- {
- // Accept a single command-line argument
- if (argc != 2)
- {
- printf("Usage: ./recover FILE\n");
- return 1;
- }
- // Open the memory card
- FILE *card = fopen(argv[1], "r");
- // Create a buffer for a block of data
- BYTE buffer[512];
- int count = 0;
- char *filename = "000.jpg";
- FILE *img = fopen(filename, "w");
- printf("Initialized\n");
- // While there's still data left to read from the memory card
- while (fread(buffer, 1, 512, card) == 512)
- {
- // if start of jpeg
- if (buffer[0] == 0xFF && buffer[1] == 0xD8 && buffer[2] == 0xFF && (buffer[3] & 0xF0) == 0xE0)
- {
- printf("line 39\n");
- // if not first jpeg
- if (count)
- {
- // close current file
- printf("entered count if\n");
- fclose(img);
- printf("closed img\n");
- // open new file "###.jpg"
- sprintf(filename, "%03i.jpg", count);
- printf("printed to filename %s\n", filename);
- img = fopen(filename, "w");
- printf("opened %s\n", filename);
- }
- printf("line 53\n");
- count++;
- }
- // else
- else
- {
- printf("line 59\n");
- // if already found jpeg
- if (count)
- {
- //keep writing to it
- fwrite(buffer, 1, 512, img);
- }
- }
- }
- // close remaining files
- fclose(img);
- fclose(card);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement