0

Here is my code (albeit a draft) I can't seem to figure out how to move forward from this error ..

int main(int argc, char* argv[]) 
{

    typedef uint8_t  BYTE;

    //card.raw is in 512 byte blocks.
    BYTE buffer[512];

    // open input file 
    FILE* inptr = fopen("card.raw", "r");

    if (inptr == NULL)
    {
        printf("Could not open %s.\n", "card.raw");
        return 2;
    }

    char title[7];
    int a = 0;
    int b = 0;
    sprintf(title, "000.jpg");
    FILE* img = fopen(title, "w");

    while (fread(&buffer, 512, 1, inptr) == 1)
    {

        if ( buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff
            && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            if (a > 9)
            {
                b++;
                a = 0;
            }

            a++;
            fclose(img);
            sprintf(title, "0%d%d.jpg", b, a);
            img = fopen(title, "w");
            fwrite(&buffer, 512, 1, img);

        }

        else
        {
            fwrite(&buffer, 512, 1, img);
        }
    }

    fclose(inptr);
    fclose(img);
}
1

The problem lies here:

char title[7];

The file name is 7 characters long. Unfortunately, you didn't leave room for the end of string marker \0. This is one of those problems that doesn't make itself clearly known when it causes a seg fault.

However, there's another problem coming that is directly related to this, but let's see if you figure it out on your own. ;-)

All that said, there are more issues, but that wasn't your question. Once you've had a chance to work on them, if you still need help, then a new question is in order.

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 .