I'm not sure what I'm doing wrong. I tried to follow the walkthrough. My code is below. Any help will be appreciated.
typedef uint8_t BYTE; typedef struct { BYTE first; BYTE second; BYTE third; BYTE fourth; }SIGNATURE;
int main(int argc, char* argv[]) {
// USE BUFFER? // FIGURE OUT SPRINTF // START JPEG LABEL AT 0
// read in chunks of 512 bytes
// compare to 0xff 0xd8 0xff 0xe0 || 0xff 0xd8 0xff 0xe1
// if matched, start new file
// if doesn't, continue old file
// name file using sprintf
// when ends, out as a jpg file
// compare and find
// if matched, start new file
// need char for sprint f (8 as needs null terminator)
char name [8];
FILE* file = fopen("card.raw", "r");
// open first output file
// str name = sprintf(name, "%3d.jpg", 0); FILE* outptr = fopen(name, "w"); if (outptr == NULL)
{
fclose(file);
fprintf(stderr, "Could not create %s.\n", name);
return 3;
}
// temp storage
SIGNATURE sig;
// read signature from infile
fread(&sig, sizeof(SIGNATURE), 1, file);
// compare to 0xff 0xd8 0xff 0xe0 || 0xff 0xd8 0xff 0xe1
if (sig.first == 0xff && sig.second == 0xd8 && sig.third == 0xff && (sig.fourth==0xe0 || sig.fourth == 0xe1))
{
fwrite(&sig, 1, sizeof(SIGNATURE), outptr);
fread(&sig, 1, 512 - sizeof(SIGNATURE), file);
fwrite(&sig, 1, 512 - sizeof(SIGNATURE), outptr);
int file_name = 0;
while (!feof(file)) // while (fgets(output, sizeof(output), file) != NULL)
{
//read in signature
fread(&sig, sizeof(SIGNATURE), 1, file);
//if not equal to, keep reading and writing
if (!(sig.first == 0xff && sig.second == 0xd8 && sig.third == 0xff && (sig.fourth==0xe0 || sig.fourth == 0xe1)))
{
// write the signature
fwrite(&sig, 1, sizeof(SIGNATURE), outptr);
// read and then write the remaining bytes in 512 block
fread(&sig, 1, 512 - sizeof(SIGNATURE), file);
fwrite(&sig, 1, 512 - sizeof(SIGNATURE), outptr);
}
else
{
// close the first file
fclose(outptr);
file_name++;
char title [8];
// convert file name to char
sprintf(title, "%3d.jpg", file_name);
// open new file
FILE* outptr = fopen(title, "w");
// write the signature
fwrite(&sig, 1, sizeof(SIGNATURE), outptr);
// read and then write the remaining bytes in 512 block
fread(&sig, 1, 512 - sizeof(SIGNATURE), file);
fwrite(&sig, 1, 512 - sizeof(SIGNATURE), outptr);
}
}
}
// close infile
fclose(file);
// close outfile
fclose(outptr);
}