0

I've made a program that outputs 50 viewable jpegs and names them 000.jpg through to 049.jpg but I am not passing check50, here's what the tests say:

This is what valgrind returns:

HEAP SUMMARY:
==16547==     in use at exit: 0 bytes in 0 blocks
==16547==   total heap usage: 51 allocs, 51 frees, 28,968 bytes allocated
==16547== 
==16547== All heap blocks were freed -- no leaks are possible

Check50 returns:

:( recovers 000.jpg correctly
   \ killed by server
:( recovers middle files correctly
   \ killed by server
:( recovers last file correctly
   \ killed by server

I'm not sure where I'm going wrong or if I'm going into an eternal loop somewhere, I've put in all my code here because I don't think it would make much sense if I just put in chunks, any help would be greatly appreciated, thanks guys :)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>

#define TRUE 1 
#define FALSE 0

typedef uint8_t  BYTE;



bool isJpeg(BYTE buffer[512])
{
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)
    {
        if (buffer[3] >= 224 && buffer[3] <= 239)
            return TRUE;
    }
    return FALSE;
}

int main(int argc, char* argv[])
{
    // Open memory card file
    BYTE buffer[512];
    FILE* card = fopen("card.raw", "r");
    if (card == NULL)
    {
        printf("File not found.\n");
        return 1;
    }

    //read until jpeg signature found
    do
    {
        fread(&buffer, sizeof(buffer), 1, card);
    }    
    while (isJpeg(buffer) == FALSE);


    //recover 50 jpeg files
    int count = 0;
    while (count < 50)
    {
        char jpeg[8]; //don't forget to include space in memory for \0
        sprintf(jpeg,"%03i.jpg", count);
        FILE* image;
        image = fopen(jpeg, "w");

        //write the recently read block to the currently opened JPG (if any)
        if (count < 49)
        {
            do
            {
                fwrite(buffer, sizeof(buffer), 1, image);
                fread(&buffer, sizeof(buffer), 1, card);
            }
            while ( isJpeg(buffer) == FALSE );
        }

        if (count == 49)
        {
            do
            {
                fwrite(buffer, sizeof(buffer), 1, image);
            }    
            while (fread(&buffer, sizeof(buffer), 1, card) == 1);
        }
        count++;
        fclose(image);
    }
    fclose(card);
}   
3

Your code has a design flaw that you didn't anticipate. It is hardcoded for 50 files. Unfortunately, the test file that check50 uses has a different number of images in it. The result is that your program goes into an infinite loop looking for the next signature that never comes. It hits the EOF of the input file and keeps reprocessing the last block that was left in your buffer, increasing the last image file without end.

Check50 has a timer that aborts programs that are too slow or go into an infinite loop. It is engaging on your program.

You need to rewrite your program for an undetermined number of image files in the input file.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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