0

Alrighty. I just need someone to point out to me where the issue is so i can finish this thing. submit it. and move on. i'm so so close. so help would be greatly appreciated.

// include libraries
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// data type BYTE
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    // open card
    FILE *card = fopen(argv[1], "r");

    // check fpointer is not NULL
    if (card == NULL)
    {
        return 1;
    }

    // variables
    int count = 0;
    BYTE buffer[512];
    char name[8];

    // open & name first img file
    sprintf(name, "%03i.jpg", count);
    FILE *img = fopen(name, "w");

    while (fread(buffer, 512, 1, card) == 1)
    {
        // start of a new jpg
        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] & 0xf0) == 0xe0)
        {
            // if first jpg
            if (count == 0)
            {
                count++;

                fwrite(buffer, 512, 1, img);
            }
            // if not
            else
            {
                // close img file
                fclose(img);

                // open & name new img file
                sprintf(name, "%03i.jpg", count);
                img = fopen(name, "w");

                count++;

                fwrite(buffer, 512, 1, img);
            }
        }
        else
        {
            // if file is open
            if (ftell(img) >= 0)
            {
                fwrite(buffer, 512, 1, img);
            }
        }
    }
    fclose(img);
    fclose(card);
}
1

So let's look at the code that handles the non-signature blocks.

    else
    {
        // if file is open
        if (ftell(img) >= 0)
        {
            fwrite(buffer, 512, 1, img);
        }
    }

It's supposed to discard the initial garbage blocks and write out blocks after the first signature is found. But what's really happening?

First, the code opens the first output file for write. Then it reads the first block of data (which is garbage and should be thrown away.)

The test is if (ftell(img) >= 0). But if the file is just opened and nothing has been written to it, wouldn't ftell(img) return 0? What happens then?????

BTW, did you actually look at the first 4 bytes of 001.jpg instead of just trying to open the image??? The answers are always in the raw data.

Happy coding! :-)

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

1
  • Thank you so much for your help. – Ash Apr 7 '20 at 9:47

You must log in to answer this question.

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