I have been trying to think of a way to also use for loop in addition to the do while loop version. I feel logically it could work, however there might be a lot of implementations that are bad and I can't get it to work. It compiles and runs but no files are created. Can anyone point out some major flaws for me please?
Thank you so much!
char* infile = argv[1];
char filename[6];
// open input file
FILE* raw = fopen(infile, "r");
if (raw == NULL)
{
printf("Could not open %s.\n", infile);
return 2;
}
FILE*outfile = NULL;
// Create a buffer
unsigned char Buffer[512];
for (int i = 0; i < sizeof(raw)/512; i++)
{
int counter = 0;
fread(&Buffer, 512, 1, raw);
if (Buffer[i] == 0xff && Buffer[i+1] == 0xd8 && Buffer[i+2] == 0xff && (Buffer[i+3] == 0xe0 || Buffer[i+3] == 0xe1))
{
counter++;
sprintf (filename, "%d.jpg", counter-1);
if (filename[i] != filename[i-1])
{
outfile = fopen(filename, "w");
}
}
if (counter > 0 && i%512=0)
{
fwrite(&Buffer, 512, 1, outfile);
}
}