0

When I compile my program, clang is probably not working. I get this when I compile:

~/pset3/recover/ $ make recover
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    recover.c  -lcrypt -lcs50 -lm -o recover
recover.c:43:34: error: use of undeclared identifier 'out'
            fwrite(temp, 1, 512, out);
                                 ^
1 error generated.
<builtin>: recipe for target 'recover' failed
make: *** [recover] Error 1
~/pset3/recover/ $ 

And here is where the error is:

    while (!endOfFile)
    {
        fread(temp, 1, 512, card);
        if (fread(temp, 1, 512, card) != 512)
        {
            endOfFile = true;
            break;
        }
        if (temp[0] == 0xff & temp[1] == 0xd8 & temp[2] == 0xff &&
        (temp[3] <= 0xe0 & temp[3] >= 0xef))
        {
            sprintf(fname, "%03i.jpg", fileCount);
            FILE *out = fopen(fname, "w");
            foundFile = true;
            fileCount++;
        }
        if (foundFile)
        {
            fwrite(temp, 1, 512, out);   //Error HERE
        }
    }

As you can see, out was defined 6 lines before the error, so clang is probably broken or something. 🙁

1

The variable out is a local variable and in C the local variables are block scoped and hence the variable out in this case only exists within the block;

{
    sprintf(fname, "%03i.jpg", fileCount);
    FILE *out = fopen(fname, "w");
    foundFile = true;
    fileCount++;
}

Simpy move the variable declaration FILE *out to the top resembling an equivalent to;

while (!endOfFile) {

  FILE *out;

  ...

}

You must log in to answer this question.

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