Below is my working code but I've a question.
If change my char filename[8]
to char filename[7]
the counter does not increase. Instead, I'll only have 2 output files: 000.jpg and 001.jpg, where 001.jpg is actually the last photo i.e 049.jpg. the printf("%i\n",counter)
will print 50 "1". Why does the size of my char/string affect the counter?
#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
FILE *inptr = fopen(argv[1], "r");
FILE *outptr = NULL;
if (inptr == NULL) //inptr corresponds to the file your program reads
{
fprintf(stderr, "Could not open %s.\n", argv[1]); //fprintf(stderr,""); Prints whatever is provided within the quotes, to the console.
return 2;
}
BYTE block[512];
int counter = 0;
char filename[8]; //xxx.jpg\n need 8 space including terminating char
while(1)
{
//fread(data, size, number, inptr) size_t is the return type of sizeof() which fread return. fread returns number of bytes read
//sizeof(BYTE) is actually just 1. fread function's size is the number of bytes to read.
size_t bytesread = fread(block, sizeof(BYTE), 512, inptr);
if (bytesread == 0 && outptr != NULL) //if fread nothing break and currently no files open
{
fclose(outptr);
fclose(inptr);
break;
}
if((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0) && outptr != NULL)
{
fclose(outptr);
counter++;
printf("%i\n",counter);
}
if((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0))
{
sprintf(filename, "%03i.jpg", counter); //%03i means 3 integer with zero padding, sprinf saves the 2nd argument to the 1st
outptr = fopen(filename, "w");
}
if (outptr != NULL)
{
fwrite(block, 1, bytesread, outptr);
}
}
return 0;
}