我有重新定义“fptr”的问题

时间:2016-12-02 16:18:37

标签: c syntax scope function-call

第15行是我无法修复的。请善意查看我的代码并诊断问题。我是编程新手,非常感谢任何指点我正确方向的人。

#include <stdio.h>
#include <stdlib.h>

#define TEAMS 200
#define RUNNERS 10000
#define LENGTH 20
#define TEAMSIZE 50

FILE *fptr;
fptr = fopen("myfile.txt","w");
void getdetails();

struct person {
    char name[LENGTH];
    int number;
    int age;
    int event;
    float money;
    float time;
}p;

struct team {
    char tname[LENGTH];
    int nummembers;
    float money;
    struct person members[TEAMSIZE];
}t;

int main() {

    int c,flag=0,i=0,j,k=0;

    printf("\n---------------------------------------------------");
    printf("\n---------------------------------------------------");
    printf("\nHeader Specification");

    while(flag==0) {
        printf("\n1.Individual Registration");
        printf("\n2.Team Registration");
        printf("\n3.Running Events");
        printf("\n4.Donation Totals");
        printf("\n5.Exit");

        printf("\nEnter your choice:");
        scanf("%d",&c);

        switch(c) {
        case 1:
            printf("\n For Individual Registration");
            printf("\n1.Early Registration");
            printf("\n2.regular Registration");
            int ch;

            printf("\nEnter your choice:");
            scanf("%d",&ch);

            switch(ch) {
            case 1: 
                printf("\n For Early Registration");
                i=i+1;
                getdetails(i);
                break;
            case 2: 
                printf("\n For Early Registration");
                i=i+1;
                getdetails(i);
                break;

            default: 
                printf("\n not valid");
                break;
            }
            break;
        case 2:
            printf("\n For Team Registration");

            printf("\n Enter team name:");
            scanf("%s",t.tname);

            printf("\n Enter team participant number:");
            scanf("%d",&t.nummembers);

            k=k+1;

            for(j=1;j<=t.nummembers;j++) {
                getdetails(k);
            }
            break;
        case 5:
            flag=1;
            break;
        }
    }
     return 0;
}

void getdetails(int i) {

    printf("Enter your name:");
    scanf("%s",p.name);

    printf("Enter your age:");
    scanf("%d",&p.age);

    printf("Enter the event:");
    scanf("%d",&p.event);

    printf("Enter the donation amount:");
    scanf("%f",&p.money);

    if(fptr == NULL) {
       printf("Error!");
       exit(1);
    }

    fprintf(fptr,"\n%s register for\t%dk race\tand the number is%d.",p.name,p.event,i);
    fclose(fptr);
}

1 个答案:

答案 0 :(得分:2)

运行时可执行语句,如

 fptr = fopen("myfile.txt","w");

不能驻留在全球范围内。它必须驻留在块范围内,即在某个函数体中。