Recover: 課題
メモリカードに保存されていた画像ファイルを復元する
https://cs50.harvard.edu/x/2021/psets/4/recover/
Recover: 回答例
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover card.raw");
return 1;
}
// Open file for reading
FILE *input_file = fopen(argv[1], "r");
// Input_file pointer fail to open then REturn error code "Could not open file"
if (input_file == NULL)
{
printf("Could not open file");
return 2;
}
// Declare a variable to unsigned char to store 512 chunks array
unsigned char buffer[512];
// For the purpose of counting of image later in the loop
int count_image = 0;
// An uninitialize file pointer to use to output data gotten from input file
FILE *output_file = NULL;
// Allocating memory to filename
char *filename = malloc(8 * sizeof(char));
// Read 512 bytes from input_file and store on the buffer
while (fread(buffer, sizeof(char), 512, input_file))
{
// Check if bytes is start of a JPEG
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// Write jpeg into file name in form 001.jpg, 002.jpg and so on
sprintf(filename, "%03i.jpg", count_image);
// Open Out_file for writing
output_file = fopen(filename, "w");
// Count number of image found
count_image++;
}
// Check if output have been used for valid input
if (output_file != NULL)
{
fwrite(buffer, sizeof(char), 512, output_file);
}
}
free(filename);
fclose(output_file);
fclose(input_file);
return 0;
}
未希 諒
香港在住の34歳サラリーマン。戦略家、写真家、旅人、サウナー。 2025年までに季節を選びながら世界中を飛び回る仕事をするために準備中。その道中で発見した気づきや気になる話題を中心に発信しています。2010年より旅をスタートし通算500箇所の宿に宿泊。年間の3分の1をホテルで暮らす。
Related posts
About media
Ramps Blog は、ノマドによるノマドのためのブログです。日本、香港、シンガポール、タイ、台湾のノマド生活を通じて見えた景色、特に食、カルチャー、テクノロジー、ビジネス、アート、デザインなどについて発信しています。
Recent Posts
Twitter feed is not available at the moment.