I keep trying to compile this code and it tells me "img" is an undeclared identifier. If I try to declare it, it tells me I'm shadowing a local variable. I'm not sure what the solution is.
I have not been able to run this code, so I'm sure it's riddled with other problems as well. Any help would be appreciated. Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover infile\n");
return 1;
}
// open input file
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
BYTE buffer [512];
// iterate over infiles bytes
// create int to track jpeg file names
int n = 0;
char *filename = 0;
// read 512 bytes
while(!feof(file))
{
fread(buffer, 1, 512, file);
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
// check for first jpeg on memory card
if (n == 0)
{
// open first jpeg file
sprintf(filename, "%03i.jpg", n);
FILE* img = fopen(filename, "w");
fwrite(buffer, 512, 1, img);
n++;
}
// check for all subsequent jpegs on memory cards
if (n > 0)
{
// close open jpeg file
fclose(img);
// open next jpeg file
sprintf(filename, "%03i.jpg", n);
FILE* img = fopen(filename, "w");
fwrite(buffer, 512, 1, img);
n++;
}
}
else if (n > 0)
{
fwrite(buffer, 512, 1, img);
}
}
// close files
fclose (img);
fclose (file);
// success
return 0;
}