0

I have come up with this code for recover but there is a segmentation fault I can't figure out where. any pointers welcome.

BYTE buffer[512];
    int i = 0;
    FILE *jpeg = NULL;
    while (fread(buffer, sizeof(BYTE), 512, card) == 512)
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] &0xf0) == 0xe0)
        {
            if (i == 0)
            {
                char name[8];
                sprintf(name,"%03i.jpg", i);
                i++;

                jpeg = fopen(name, "w");

                if (jpeg == NULL)
                {
                    printf("Could not open file.\n");
                    return 1;
                }

                fwrite(buffer, sizeof(BYTE), 512, jpeg);

            }
            else
            {
                fclose(jpeg);
                char name[8];
                sprintf(name,"%03i.jpg", i);
                i++;
                jpeg = fopen(name,"w");
                if (jpeg == NULL)
                {
                    printf("Could not open file.\n");
                    return 1;
                }
                fwrite(buffer, sizeof(BYTE), 512, jpeg);

            }

        }
        else
        {
            fwrite(buffer, sizeof(BYTE), 512, jpeg);
            fclose(jpeg);
        }

    }

    fclose(card);

1 Answer 1

0

try running the program with valgrind. It will tell you which line is throwing the seg fault.

BTW, you did open the input file "card", right?

You must log in to answer this question.

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