Created
October 13, 2017 16:47
-
-
Save belogical/cc5c888d850aaac08a54c21536e935c0 to your computer and use it in GitHub Desktop.
Recover.c in pset4 of cs50
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
#include<stdio.h> | |
#include<stdlib.h> | |
#include<stdint.h> | |
int main(int argc,char *argv[]) | |
{ | |
if(argc!=2) | |
{ | |
fprintf(stderr,"usage: ./recover infile"); | |
return 1; | |
} | |
uint8_t block; | |
int d=0; | |
unsigned char data[512]; | |
char *buffer=argv[1]; | |
FILE *inptr=fopen(buffer,"r"); | |
if(inptr==NULL) | |
{ | |
fprintf(stderr,"could not open %s",buffer); | |
fclose(inptr); | |
return 2; | |
} | |
char ch; | |
size_t no; | |
while(fread(data,(sizeof(block)),512,inptr)==512) | |
{ | |
if(data[0]==0xff&&data[1]==0xd8&&data[2]==0xff&&(data[3]==0xe0||data[3]==0xe1)) | |
{ | |
char arr[8]; | |
sprintf(arr,"%03d.jpg",d); | |
d++; | |
FILE *img=fopen(arr,"w"); | |
do | |
{ | |
fwrite(data,1,(sizeof(block)*512),img); | |
no= fread(data,sizeof(block),512,inptr); | |
} | |
while(((data[0]!=0xff) && (data[1]!=0xd8) && (data[2]!=0xff)) && ((data[3]!=0xe0) || (data[3]!=0xe1))&&(no==512)); | |
ch=fgetc(inptr); | |
printf("%c",ch); | |
fclose(img); | |
fseek(inptr,-(sizeof(block)*512),SEEK_CUR); | |
} | |
} | |
fclose(inptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment