I'm sorry for being vague, but I don't know up from down where recover's concerned. I'm trying to stick to the pseudo code and believe I've reached the point where I want to test if a jpg's already been found, but:
- I don't believe my code is structured correctly. Rather than skip over the leading 512 blocks, I expect my code tries to write them because I use else. Makes you think I could fix it, but if I don't close the if loop, it doesn't go back to get the next 512 block, which makes me question whether the while loop is badly done. I don't know if it could work because there's no file open it can write to until the if condition evaluates to true.
I was surprised I had to move the file ptr for the jpgs outside of the while loop to resolve an error. I believe it was because the else loop couldn't see it, but I'd love to be certain and learn a better way.
I used EOF in my while loop because of an example in the shorts for week 4 and therefore thought it was an ok practice, but in searching stack exchange I've stumbled on posts re: EOF not working well in while loops which were beyond me, but made me question whether it's a mistake.
To test whether a jpg's already been found meant if condition to me. I don't know how to code it. I've written "if jpgcount was incremented," but can't see how "if (jpgcount = jpgcount + 1)" can work. Even tried defining one in terms of the total number of jpgs.
#include <stdio.h> #include <stdint.h> typedef uint8_t BYTE; int main(int argc, char *argv[]) { // program accepts 1 CLA if (argc != 2) { fprintf(stderr, "Usage: ./recover image"); return 1; } // open image file FILE *image_ptr = fopen(argv[1], "r"); // checks imagee_ptr isn't null and exits if is if (image_ptr == NULL) { fprintf(stderr, "Can't open image file\n"); return 2; } // buffer to store blocks from image file BYTE block[512]; // ptr for jpg files FILE *jpgrec_ptr = NULL; // variable to count number of jpgs found int jpg_count = 0; // read 512 bytes into a buffer while ((fread(&block, sizeof(BYTE), 512, image_ptr)) != EOF) { // checks block's 1st 4 bytes for signature jpg header if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0) { // array to store jpeg filename char filename[8]; // create filename for jpg sprintf(filename, "%03i.jpg", jpg_count); // open jpg file for writing jpgrec_ptr = fopen(filename, "w"); // write block to jpg file fwrite(&block, sizeof(BYTE), 512, jpgrec_ptr); // jpg found, increase jpg_count by 1 jpg_count++; } // writes block to jpg file else { fwrite(&block, sizeof(BYTE), 512, jpgrec_ptr); } // has a jpg been found? // if jpg_count has increased by 1... // if (jpg_count = jpg_count + 1) }
I'm sorry for being so entry-level and I do get I'm asking for an extraordinary amount of patience. I was really doing ok until recover and then the bottom just dropped from under me. My thanks for any help .