Last active
February 24, 2022 10:25
-
-
Save teeschorle/cf5ef31bb26d9da5a85e2c71f31f494c to your computer and use it in GitHub Desktop.
CS50 Problem Set 4 (Spring 2020) - Recover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CS50 Problem Set 4 (edx 2020): Recover | |
//Author: teeschorle | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
int main(int argc, char *argv[]) | |
{ | |
// define datatype BYTE (why not char: chars are not necessarily exactly 8 bits long) | |
typedef uint8_t BYTE; | |
// test for correct command line input | |
if (argc != 2) | |
{ | |
printf("Usage: ./recover image"); | |
return 1; | |
} | |
char *nameofrawfile = argv[1]; | |
// open raw file | |
FILE *rawfile_ptr = fopen(nameofrawfile, "r"); | |
// test if file exists | |
if (rawfile_ptr == NULL) | |
{ | |
printf("Could not open %s.\n", nameofrawfile); | |
return 1; | |
} | |
BYTE buffer[512]; //to store the currently read chunk | |
//counter and title to name jpeg | |
int counter = 0; | |
char title[8]; | |
bool open = false; | |
FILE *image_ptr; | |
//for (int i = 0; i < 500; i++) | |
while (fread(&buffer, 512, 1, rawfile_ptr) > 0) //the reading already takes place in the while condition | |
{ | |
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 224) == 0xe0) ) //(224 = 0xe0 = 11100000 in binary) | |
{ | |
sprintf(title, "%d%d%d.jpg", counter / 100, (counter % 100) / 10, counter % 10); | |
// printf(title[7] == '\0' ? "true" : "false"); //in order to verify that sprintf includes terminating null character (which it does) | |
image_ptr = fopen(title, "w"); | |
open = true; | |
fwrite(buffer, 512, 1, image_ptr); | |
counter++; | |
} | |
else if (open == true) | |
{ | |
// FILE *image = fopen(title, "a"); //a => append | |
fwrite(buffer, 512, 1, image_ptr); | |
} | |
} | |
fclose(image_ptr); | |
fclose(rawfile_ptr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe cause I'm new to coding, but I find this way too confusing...