0

I need some direction as to what I should do next in order to make my program work. The issue is that my my code doesn't generate any JPEGS in the current directory. I've been working on this for a week and would really appreciate someone's help! Thanks

// declaring buffer array
int buffer[512];
int counter = 0; 
FILE* img = NULL;

// open file
FILE* card = fopen("card.raw", "r");
if (card == NULL)
{
    printf("Could not open %s.\n", "card.raw");
    return 1;
}

// find JPEG 
while (!feof(card))
{   
    // read card and temporary store infomration in buffer 
    fread (&buffer, sizeof(buffer), 1, card);

    // if 4-byte JPEG sequence is found, do this loop 
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1 || buffer[3] == 0xe2 || buffer[3] == 0xe3 || buffer[3] == 0xe4 || buffer[3] == 0xe5 || buffer[3] == 0xe6 || buffer[3] == 0xe7 || buffer[3] == 0xe8 || buffer[3] == 0xe9 || buffer[3] == 0xea || buffer[3] == 0xeb || buffer[3] == 0xec || buffer[3] == 0xed || buffer[3] == 0xee || buffer[3] == 0xef))
    {
        char title [8]; 

        // if this is the first JPEG sequence  
        if (counter == 0) 
        {
            sprintf (title, "%03d.jpg", counter);

            // open new file 
            img = fopen (title, "a");
            fwrite (&buffer, sizeof(buffer), 1, img);
            counter++;  
        }

        else 
            sprintf (title, "%03d.jpg", counter);
            fwrite (&buffer, sizeof(buffer), 1, img);
            counter++; 

    }

}

fclose (card); 

return 0;
0

Actually, it's generating one file, but not correctly. There are several issues that need to be corrected (mostly because the code hasn't been written to handle these issues). First, when the code detects a signature block, it is writing that block to the one file.

Next, the blocks that follow, which don't have a signature, are not being written out to the currently open jpeg file. When a new signature block is detected, the current file needs to be closed (if one has been opened), and a new file opened.

Finally, there's an issue with how the EOF is handled. Simply put, it's going to process one extra block.

There may be more issues, but this will get you going. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • Okay so I made a do while loop within the jpeg sequence indicator loop. This do while loop will keep on adding more bytes to the current opened jpeg file until another jpeg sequence is found - does this make sense? And can you direct me to a source explaining why my program is creating one extra block? I wasn't aware. Thank you for your help so far!!!! Sep 17 '16 at 21:16
  • After doing some more research I figured out how to stop the program from processing one extra block :) Sep 17 '16 at 21:32
  • I made a while (true) loop and within that have if (!feof(card)), along with the rest of the code that follows. This works, but I don't understand why. Can you explain why?? Sep 17 '16 at 22:01

You must log in to answer this question.

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