#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
int main(int argc, char *argv[])
{
//this counter to keep track of how many jpegs found so far
int counter = 0;
//allocate a place with 8 characters for inputting the filename
char filename[8];
//replace argv[1] with card
char *card = argv[1];
//create a pointer called img
FILE *img;
//check that if user did not give any inputs, to return
if(argc !=2)
{
printf("Usage: ./recover image \n");
return 1;
}
//open up file and read the file
FILE *file = fopen(card,"r");
if(!file)
{
printf("Could not open card\n");
return 1;
}
//declare a buffer
unsigned char buffer[512];
//iterate over contents of the file till the end of the file
while (fread(buffer, 512, 1, file) == 512)
{
//if first 4 bys of the array are as per the below
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//if this is the 1st JPEG that someone is working on
if(counter == 0)
{
//create a new jpeg file
sprintf(filename, "%03i.jpg", counter);
counter++;
//open this new jpeg file
img = fopen(filename, "w");
//start writing to this new jpeg, need a number that is based on how many times before the next "start" of the jpeg file
fwrite(buffer, 512, 1, img);
}
//if this is not the 1st JPEG
if(counter > 0)
{
//close the file at location img that was being written to
fclose(img);
//create a new jpeg file
sprintf(filename, "%03i.jpg", counter);
counter++;
//open this new jpeg file
img = fopen(filename, "w");
//start writing to this new jpeg, need a number that is based on how many times before the next "start" of the jpeg file
fwrite(buffer, 512, 1, img);
}
}
//to continue writing if there was a JPEG found before previously
else if (counter > 0 && fread(buffer, 512, 1, file) == 512)
{
//continue writing to this new jpeg
fwrite(buffer, 512, 1, img);
}
//in case the first 512 bytes are not a jpeg, to ignore and continue with next set of 512 bytes
else
{
continue;
}
}
//close file
fclose(img);
fclose(file);
}
-
Have you identified which line is creating the seg fault yet? This is a very critical skill that you need to develop.– Cliff BCommented Jun 24, 2020 at 3:45
Add a comment
|
1 Answer
change while (fread(buffer, 512, 1, file) == 512)
to
size_t rd = 0;
while ((rd=fread(buffer, 1, 512, file))) {
// now you have read rd bytes
}
-
thank you but i'm trying to understand this please? i thought if fread returns whatever things they read, anytime that they don't return 512 means they've reached end of the file? In this case if we assgign rd = 0, are we assign it to fread?– DarylCommented Jun 24, 2020 at 7:02
-
fread(buffer, 512, 1, file)
will never return 512, it will return 1 or 0.rd=0
is to initialize rd with 0, it will not affect fread.– stensalCommented Jun 24, 2020 at 14:25 -
fread will return a number between 0 and the value of the THIRD parameter, depending on how many elements it reads. (The second parameter is the size of one element.) You could use
while (fread(buffer, 512, 1, file) == 1)
orwhile (fread(buffer, 1, 512, file) == 512)
The former tells you if you have reached EOF when it is 0. The latter will do that, plus, with a little more code, it will tell you if it does a partial read.– Cliff BCommented Jun 24, 2020 at 21:09