include <stdio.h>
include <stdlib.h>
typedef uint8_t byte;
define SI 512
int main(int argc, char* argv[]) {
FILE* card = fopen("card.raw", "r");
if (card == NULL)
{
fclose(card);
printf("Unable to open a file\n");
return 1;
}
byte buffer[SI];
char title[8];
int counter = 0;
FILE* pic = NULL;
//when card can be read
while(fread(buffer,sizeof(byte),SI,card) == SI)
{
//check for jpg
if((buffer[0] == 0xFF && buffer[1] == 0xd8 && buffer[2] == 0xff) && buffer[3] >= 0xe0 && buffer[3] <= 0xef) {
if(open == 1)
{
open = 0;
fclose(pic);
}
sprintf(title,"%03d.jpg",counter);
//open a first picture file to write into
pic = fopen(title, "a");
//check if the file exist
if(pic == NULL)
{
printf("This file doesnt exist\n");
fclose(pic);
return 2;
}
//write the beginning of the first file into pic file
open = 1;
counter++;
//write the rest of bytes
}
//if file is open write in bytes Blocks
if(open == 1)
{
fwrite(buffer,sizeof(byte),SI,pic);
}
}
fclose(pic);
fclose(card);
return 0;
}