I can retrieve all 50 photos, it works as far as I can see, but check50 won't let me pass. It says I don't recover any file correctly, it expects an exit code of 0 and my code doesn't produce that. I use a separate function to find the beginning of the first image, two file pointers are opened and closed. Then I open file pointers to write the first chunk of 512 bytes. I close the file pointers and reopen them for the rest. I use a while-loop and I open and close file pointers each time the code runs through the loop. So I guess, too many file pointers. But I have worked hard on this and I just don't know what else to do.
typedef uint8_t BYTE;
int findFirstImage(char* fotoFile);
int main(int argc, char *argv[]) {
if(argc != 2) {
fprintf(stderr, "Please enter one single file name");
return 1;
}
char *fotoFile = argv[1];
// open the file, read until you find the beginning of a sentence
int image = findFirstImage(fotoFile);
printf("image %i\n", image);
BYTE writeImage[512] = {0};
FILE* secondinptr = fopen(fotoFile, "r");
if (secondinptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", fotoFile);
return 2;
}
fseek(secondinptr, (image - 4), SEEK_CUR);
fread(writeImage, 1, 512, secondinptr);
fclose(secondinptr);
// create name of file that will be written into
char name[7];
int tekst = 0;
sprintf(name, "%03i.jpg", tekst);
//copy contents from beginning to end of first sentence in f
FILE* outptr = fopen(name, "w");
if (outptr == NULL)
{
fclose(secondinptr);
fprintf(stderr, "Could not create %s.\n", name);
return 3;
}
fwrite(writeImage, 1, 512, outptr);
fclose(outptr);
FILE* thirdinptr = fopen(fotoFile, "r");
if (thirdinptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", fotoFile);
return 2;
}
int counter = 512;
while(!feof(thirdinptr)) {
BYTE writeImage1[512] = {0};
// set pointer at next chunk of code
fseek(thirdinptr, (image - 4 + counter), SEEK_SET);
fread(writeImage1, 1, 512, thirdinptr);
if(writeImage1[0] == 0xFF && writeImage1[1] == 0xd8 && writeImage1[2] ==
0xFF) {
for (int b = 224; b < 240; b++) {
if (writeImage1[3] == b) {
tekst++;
sprintf(name, "%03i.jpg", tekst);
FILE* thirdoutptr = fopen(name, "w");
if (thirdoutptr == NULL)
{
fclose(thirdinptr);
fprintf(stderr, "Could not create %s.\n", name);
return 3;
}
fwrite(writeImage1, 1, 512, thirdoutptr);
fclose(thirdoutptr);
}
}
} else {
FILE* secondoutptr = fopen(name, "a");
if (secondoutptr == NULL)
{
fclose(thirdinptr);
fprintf(stderr, "Could not create %s.\n", name);
return 3;
}
fwrite(writeImage1, 1, 512, secondoutptr);
fclose(secondoutptr);
}
counter += 512;
}
fclose(thirdinptr);
}
// open the file, read until you find the beginning of a sentence
int findFirstImage(char* fotoFile) {
FILE* inptr = fopen(fotoFile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", fotoFile);
return 2;
}
BYTE buffer[512] = {};
while(!feof(inptr)) {
fread(buffer, sizeof(BYTE), 4, inptr);
if (buffer[0] == 0xFF && buffer[1] == 0xd8 && buffer[2] == 0xFF) {
for (int b = 224; b < 240; b++) {
if (buffer[3] == b) {
int firstImage = ftell(inptr);
return firstImage;
}
}
}
}
fclose(inptr);
return 1;
}
printf("image %i\n", image);
. I didn't look at the code in more detail, but that is the proximate cause of this specific error.printf("image %i\n", image);
. Theprintf
is one causing this specific check50 failure. It is getting unexpected output from that printf. Remove it/ comment it.