so this is my second time posting about this pset, my code is below. No images will open, with the current code. However when I do get something to open it is just 1 file and it says invalid or unsupported format. When run, the program cannot pass the 'if' condition that checks for the 4 byte signature, that is where it gets stuck, and to me it suggests that it can't find the signatures in the files.
I just can't see what I have done wrong, please give me some advice, what am I missing, what am I not seeing??
Also about my code, old is the card.raw and "new" is the images being produced, well in theory. Also for my 'if' condition, after the while loop, where I look for the first 4 byte signature; I know that the spec call for 4 bytes but it seems more likely to pass with less conditions and so only the first 3 bytes are there at the moment, however I have tried with all 4 and still no result! x
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
int main(int argc, char* argv[])
{
// ensure only 1 command line argument
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
// declare variable
char new_files[8];
int JPEG_Counter = 0;
FILE* new = NULL;
// open memory card file & new outfile
FILE* old = fopen(argv[1], "r");
// check the fill isnt empty
if (old == NULL)
{
fclose(old);
fprintf(stderr, "fopen() error, retry\n");
return 2;
}
// confirm fopen() worked
eprintf("File open sucsessfully\n");
// create buffer
typedef uint8_t BYTE;
BYTE buffer[512] = {0};
// read old file into buffer
while (fread(&buffer, sizeof(buffer), 1, old) != 0)
{
// confirm fread() worked
eprintf("File read sucsessfully\n");
if (buffer[0] == 0xFF &&
buffer[1] == 0xD8 &&
buffer[2] == 0xFF)
{
sprintf(new_files, "%03d.jpg", JPEG_Counter);
JPEG_Counter++;
eprintf("Counter has reached: %d\n", JPEG_Counter);
// open new files with naming system
new = fopen(new_files, "a");
// write into new from buffer
fwrite(buffer, sizeof(buffer), 1, new);
// confirm fwrite() worked
eprintf("File wrote sucsessfully\n");
// close all files
if (feof(old))
{
fclose(old);
}
else if (feof(old))
{
fclose(new);
}
// confirm fclose() worked
eprintf("File closed sucsessfully\n");
//sucsess
return 0;
}
else
{
fclose(old);
eprintf("Files do not have first 3 byte signitures\n");
}
}
}