Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <stdint.h>
- #include <unistd.h>
- int main (int argc, string argv[])
- {
- if (argc != 2)
- {
- fprintf(stderr, "Usage: ./recover image");
- return 1;
- }
- char *readingfile = argv[1]; //name of file
- //char *outfile; do we need this here since this is going to be changing?
- FILE *memorycard = fopen (readingfile, "r"); //setting pointer for memorycard to the file we opened with the name given
- uint8_t buffer[4]; //buffer to hold first four values of the array! (for checking purposes, of course!), could this happen with char*?
- //starting off with the first four bytes of data
- printf("Location of bmp after moving 4 bytes: %li\n", ftell(memorycard));
- if (memorycard == NULL) //if any segmentation faults
- {
- fprintf(stderr, "Usage: ./recover image");
- return 2;
- }
- bool didnotfindjpg = true;
- int blocksofspace = 0;
- do //fread has size of each element to be read before number of elements
- {
- fread(buffer, 4, 1, memorycard);
- if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
- {
- didnotfindjpg = false;
- fseek(memorycard, -4, SEEK_CUR); //go to the beginning of the block we are going to read
- }
- else
- {
- fseek(memorycard, -4, SEEK_CUR);
- fseek(memorycard, 512, SEEK_CUR);
- //sort of like magnifying glass to see what we are reading
- printf("First value of BMP: %i\t, Second Value: %i\t, Third Value: %i\t, Fourth value: %i\n",buffer[0], buffer[1], buffer[2], buffer[3]);
- }
- } while (didnotfindjpg == true); //now we figured out where the file starts
- uint8_t bigbuffer[512];
- bool endofjpeg = false;
- while (endofjpeg == false) //will stop when we reach the end of the jpegfile
- {
- for (int g = 0; g < 5; g++)
- {
- int a = 10*g; //does this read within the parentheses coming next?
- printf("This is the ten's place of the number of pictures we are saving: %i",g);
- for (int x = 0 + a; x < 10 + a; x++)
- {
- char nameoffile[7];
- if (x < 10) //we need to use two digits for making file names greater than 10!
- {
- sprintf(nameoffile,"00%i.jpg", x);
- }
- if (x >=10)
- {
- sprintf(nameoffile, "0%i.jpg", x);
- }
- FILE *writehere = fopen (nameoffile, "w");
- bool startofnextfile = false;
- do
- {
- int h = fread(bigbuffer, 1, 512, memorycard);
- printf("%i\n", h);
- if (h == 512)
- {
- fwrite(bigbuffer, 1,512, writehere);
- }
- else
- {
- fclose(writehere);
- printf("HEY!");
- return 0;
- }
- //free(bigbuffer);
- //free(buffer);
- fread(buffer, 4, 1, memorycard);
- if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) //while not start of JPEG
- {
- startofnextfile = true;
- fclose(writehere); //do i close "writehere" or the file?
- fseek(memorycard, -4, SEEK_CUR);
- }
- else
- {
- fseek(memorycard, -4, SEEK_CUR);
- }
- }
- while (startofnextfile == false);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement