0

(This was also posted on reddit, but I think it's been lost, sorry)

Hi guys, currently doing recover.

I'm having trouble with using fclose on my output file pointer (jpegptr) - every time I try to compile I get this error:

recover2.c:72:24: error: variable 'jpegptr' is uninitialized when used here [-Werror,-Wuninitialized]
                fclose(jpegptr);
                       ^~~~~~~
recover2.c:37:18: note: initialize the variable 'jpegptr' to silence this warning
    FILE* jpegptr;

My code is below. I feel like I'm trying to close the jpegptr before i've opened it, but i'm not sure how to fix it.

FILE* jpegptr;

if (block[0] == 0xff &&
        block[1] == 0xd8 &&
        block[2] == 0xff &&
        (block[3] & 0xf0) == 0xe0)
        {
            if (inprogress == 0)
            {
                inprogress = 1;

                sprintf(filename, "%03i.jpg", picno);

                jpegptr = fopen(filename, "w");
                if (jpegptr == NULL)
                {
                    fprintf(stderr, "Output file cannot be made\n");
                    return 2;
                }
            }

            else
            {
                fclose(jpegptr);
                picno ++;

                sprintf(filename, "%03i.jpg", picno);

                jpegptr = fopen(filename, "w");
                if (jpegptr == NULL)
                {
                    fprintf(stderr, "Output file cannot be made\n");
                    return 2;
                }
            }

            fwrite(block, sizeof(int), 512, jpegptr);

        }

I'm sure that I could open jpegptr in main which might solve the issue, but then every-time i run the program i would get a jpeg file outputted automatically, even if no jpegs even existed on the card.

1

Sometimes the simple fix is best. Try initializing the file pointer - always a good idea anyways:

    FILE* jpegptr = NULL;

While it may be that the code will work (I'd have to test it to see), the structuring of the loops and conditions is probably making it too difficult for the compiler to see what's going on.

If you're worried that you're trying to close a file that isn't open, you can always check to see if(fileptr == NULL) fclose(fileptr);

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

You must log in to answer this question.

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