0

i am getting a timed out error but don t understand why...? also i can see all the pictures except the last one (049.jpg) ...

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

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("missing command line argument\n");
        return 1;
    }

    
    FILE *card = fopen(argv[1], "r");
    if (card == NULL)
    {
        return 1;
    }
    
    
    int count = 0;
    int card_size = sizeof(card);
    FILE *img = NULL;
    

    while (card_size <= 512)
    {
        //read 512 bytes from the file (card)
        unsigned char photos[512];
        fread(photos, 512, 1, card);
        
        //check if JPEG
        if (photos[0] == 0xff && photos[1] == 0xd8 && photos[2] == 0xff)
        {
            if ((photos[3] & 0xf0) == 0xe0)
            {
                //close previous file
                if (count > 0)
                {
                    fclose(img);
                }
                
                //create a new file 00count.jpeg 
                char pic[8];
                sprintf(pic, "%03i.jpg", count);
                img = fopen(pic, "w");
                
                //check that the file was created successfully 
                if (img == NULL)
                {
                    printf("file not created\n");
                    return 1;
                }
                
                //start writting in the file
                fwrite(photos, 512, 1, img);
                count++;
               
            }
            else
            {
                //keep writting in the current file
                fwrite(photos, 512, 1, img);
            }
            
        }
        else if (img != NULL)
        {
            //keep writting in the current file
            fwrite(photos, 512, 1, img);
        }
        card_size = card_size - 511;
    }
    
    //close all files
    fclose(img);
    fclose(card);
}
0

When the end of the input file is reached, the code doesn't handle it correctly. It just keeps running. If you look at the size of your output files, you'd see that 049.jpg is about 20 times larger than the input file.

You need to rewrite the code with a better system for ending the processing of the input file. It should be based on something like EOF of the input file or the return value generated by the fread call.

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

| improve this answer | |

You must log in to answer this question.

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