-1

I don't know why this code is wrong. Here's the code.

...

int main(void)
{
    tof type;
    char FileName[260];
    printf("enter a file's name; you want to know size of this file(Maximum 260 character)\n");
    fgets(FileName,sizeof(FileName),stdin);
    Eliminate(FileName,'\n'); /*Eliminate '\n' Because '\n' remain in FileName*/
    printf("If the file's type is text, enter 1, It is binary, enter 2");
    scanf("%d",&type);

    if(type != Text && type != Binary)
        return -1;
    else if(type == Binary)
        FILE * fp = fopen(FileName,"rb");
    else if(type == Text)
        FILE * fp = fopen(FileName,"rt");

    long FileSize = SOF(fp,type);

    if(FileSize < 0)
        printf("It exceeded the limit It can calculate(about over 2GB)");
    else
        printf("Size of the file is %d byte.",FileSize);

    fclose(fp);
    return 0;
}

int SOF(FILE * subject,const tof File_type)
{
    long InitPos = ftell(subject);
    fseek(subject,0,SEEK_SET);
    long Bite = 0;
    int * Buf; //Called Buf due to fread function, little meaning
    if(File_type == Text) {
        while(fgetc(subject) != EOF && Bite >= 0)
        //When "Bite" exceed Maximum value of long type, return negative 
        {
            Bite++;
        }
    }
    else if(File_type == Binary) {
        while(fread((void*)Buf,1,1,subject) == 1 && Bite >= 0) {
            Bite++;
        }
    }
    fseek(subject,InitPos,SEEK_SET);
    return Bite;
}

...

('SOF' retunrns with a size of a file, 'Eliminate' is the function which eliminates some charater in some string, 'Text' and 'Binary' is declared to 1 and 2 with using enum)

The error message the compiler shows is:

'fp' was not decleared in this scope

But

if(type != Text && type != Binary)
    return -1;
else if(type == Binary)
    FILE * fp = fopen(FileName,"rb");
else if(type == Text)
    FILE * fp = fopen(FileName,"rt");

This code contains the declaration of FILE pointer 'fp' apparently as you can see.

Why did this error appear?

1
  • Your code is incomplete; in particular, it seems to be missing a main() function and at least one #include. It's also not minimal - there's much in there that's irrelevant to your problem. Please edit your code so it's a minimal reproducible example of your problem (including any necessary inputs, but preferably not needing any), then we can try to reproduce and solve it. You should also read How to Ask. Commented Jan 17, 2020 at 15:31

2 Answers 2

5

You declare the file inside the if statement. As soon as you leave that scope, the declaration is lost. You should make the declaration before the if statement

2

Complement to Hack5's answer:

Instead of this:

if(type != Text && type != Binary)
    return -1;
else if(type == Binary)
    FILE * fp = fopen(FileName,"rb");
else if(type == Text)
    FILE * fp = fopen(FileName,"rt");

you want this:

FILE * fp;

if(type != Text && type != Binary)
    return -1;
else if(type == Binary)
    fp = fopen(FileName,"rb");
else if(type == Text)
    fp = fopen(FileName,"rt");

or more elegantly:

FILE * fp;

if(type != Text && type != Binary)
    return -1;
else 
    fp = fopen(FileName, type == Binary ? "rb" : "rt");

Unrelated problem:

You're going to have a problem here:

while(fread((void*)Buf,1,1,subject) == 1 && Bite >= 0) {

Buf points nowhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.