0

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;
}
3
  • 1
    check50 will complain if there is output it does not expect. Like 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. Commented Jun 12, 2017 at 1:38
  • I changed "image" into beeld (image is an integer by the way) but I've still got the same output. I'm now reworking the code and especially the conditions but I'm not there yet. Thanks anyway.
    – Karin
    Commented Jun 13, 2017 at 10:43
  • I was refering to this line printf("image %i\n", image);. The printf is one causing this specific check50 failure. It is getting unexpected output from that printf. Remove it/ comment it. Commented Jun 13, 2017 at 10:54

1 Answer 1

1

check50 is detecting output from printf("image %i\n", image);, which is causing the failure. The only screen output that is allowable is the error messages produced for bad "usage" or a file-not-found. You need to remove or at least comment that line. There could be other problems in the program, you'll have to see what check50 says.

2
  • Ok, I've got the first and middle ones right, only the last file has not been accepted yet.There's not message, the smiley is just not there yet. I can't open it either, unsupported file format. Which was no problem previously only cs5 wouldn't accept. Nevertheless, progress! Thanks a lot!
    – Karin
    Commented Jun 13, 2017 at 13:01
  • I had the same problem and it was because I was checking for EOF with feof. I changed that condition and finally I had the last picture correct
    – Endgament
    Commented Jun 14, 2017 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .