I wrote the code that logically I think should be working, it creates 50 images, however if I try to open any of them, it says
Invalid or Unsupported Image Format
also when I tested it with check50 here is what I get (I don't really understand what this means
:) recover.c exists
:) recover.c compiles
:) handles lack of forensic image
:( recovers 000.jpg correctly
\ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 154..."
:( recovers middle files correctly
\ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 154..."
:( recovers last file correctly
\ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 154..."
https://sandbox.cs50.net/checks/f098860e96f14dfa92f3099865b3d5aa
Here is my code, any help would be appreciated
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
int main (int argc, char *argv [])
{
// ensure correct usage
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover file_name\n");
return 1;
}
// remember file name
char *card = argv[1];
// open in file
FILE *in_file = fopen(card, "r");
if (in_file == NULL)
{
fprintf(stderr, "Could not open %s.\n", card);
return 2;
}
// initialize out file and needed variables
FILE *out_file = NULL;
unsigned char buffer [512];
char file_name [7];
int counter = 0;
// repeat until end of the file
while (fread(&buffer, 512, 1, in_file) == 1)
{
// is it a start of a new JPEG?
if (buffer [0] == 0xff &&
buffer [1] == 0xd8 &&
buffer [2] == 0xff &&
(buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
// if not the first image, then close the previous file first
if (out_file != NULL)
{
fclose (out_file);
}
// then creat a new file
sprintf(file_name, "%03i.jpg", counter);
fopen (file_name, "w");
if (in_file == NULL)
{
fprintf(stderr, "Could not creat %s.\n", file_name);
return 3;
}
counter++;
}
// buffer does not start with jpeg header? or a new file has just been created?
if (out_file != NULL)
{
// write to outfile
fwrite(&buffer, 512, 1, out_file);
}
}
// close all files opened
fclose(out_file);
fclose(in_file);
// success
return 0;
}