1 year ago
#255248
oxolane
CS50 Recover produces "recover" file when compiling recover.c
There is no logical errors with my code, but when I compile recover.c, a file called "recover" is produced, and when I give the command "./recover card.raw", it returns nothing to me (basically i only see "recover/ $" in my terminal).
Here is my code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define BLOCK 512
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
//reject invalid input
if (argc != 2)
{
printf("Usage: ./recover IMAGE");
return 1;
}
// Open memory card and reject if invalid file
FILE *file = fopen(argv[1], "r");
if (!file)
{
printf("!file\n");
return 2;
}
BYTE temp[512]; //allocate memory to a block (for reading)
char filename[8]; //allocate memory to filename
FILE *img; // there exists a file
int count = 0; // increase counter to read subsequent blocks
while (fread(temp, sizeof(BYTE), BLOCK , file) == 512)
{
BYTE buffer[4];
fread(buffer, sizeof(BYTE), 3 , file); //fread returns the number of items of size "sizeof(BYTE)" that were read, which is "3")
// look for beginning of a .jpeg (0xFF 0xD8 0xFF 0xe?)
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
sprintf(filename, "%03i.jpg", count);//TODO: 2 is a constant. Change later to increments. Change filename as well to increment
// open a new jpeg file, formatted as ###.jpg, starting at 000.jpg
//if loop to close the previous file (if previous file exists)
if (count > 1) //no need to close anything if first jpeg
{
fclose(img);
}
//This can be done by if counter = 0, continue searching for first jpeg, if counter != 0, close prev file and open new img
img = fopen(filename, "w");
if (!img)
{
printf("!img\n");
return 3;
}
fwrite(temp, BLOCK, 1, img);
count++; // write 512 bytes into new img file
}
else if (count > 0) //
{
fwrite(temp, BLOCK, 1, img); //write 512 bytes as a continuation of current opened jpeg file
}
}
}
Thanks in advance for helping me!
c
cs50
recover
0 Answers
Your Answer