简体   繁体   中英

How to use a file pointer in a called function in C

I wrote some code as a school exercise. We need to use structs and functions for this exercise. I want to read some date out a file with one function and save this data to a struct. Next i have another function to do an easy calculation on this data. I initialize my filepointer in the main function with fopen, the problem is i want to use the same pointer in a function called by the main. My program compiles when i declare my FILE *fp outside the main function but it crashes while running. Does anyone can help me with this? Thank you all. Here is the code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
    int dag;
    int maand;
    int jaar;
}geboortedag;

typedef struct 
{
    char name[BUFSIZ];
    geboortedag dag;
}persoon;

void readperson(persoon *);
geboortedag calculate(p);

int main(int argc, char *argv[])
{
    FILE *fp;
    persoon a;
    persoon *p;
    p = &a;
    geboortedag leeftijd;

    if (argc != 5)
    {
        printf("Het aantal argumenten die ingegeven is niet correct\n");
        getchar();
        exit(1);
    }
    else if (argc == 5)
    {
        if ((fp = fopen(argv[1], "r")) == NULL)
        {
            printf("De file kon niet worden geopend\n");
            getchar();
            exit(2);
        }
    }

    readperson(p);
    leeftijd = calculate(p);
    printf("De leeftijd is %d %d %d", leeftijd.dag, leeftijd.maand,   leeftijd.jaar);
    fclose(fp);
    printf("Het bestand is correct afegsloten");
    getchar();
    return 0;
}

void readperson(persoon *x)
{
    char a[BUFSIZ];
    fgets(a, BUFSIZ, fp);
    strcpy(x->name, a);
    fscanf("%d%d%d", x->dag.dag, x->dag.maand, x->dag.jaar);
}

geboortedag calculate(persoon *x)
{
    geboortedag tijd;
    int a, b, c;
    a = atoi(x->dag.dag);
    b = atoi(x->dag.maand);
    c = atoi(x->dag.jaar);
    tijd.dag = x->dag.dag - a;
    tijd.maand = x->dag.maand;
    tijd.jaar = x->dag.jaar;
    return tijd;
}

EDIT: So i tried what was suggested here. I'm still getting an error about the filepointer that might not be initialized...

Error C4703 potentially uninitialized local pointer variable 'fp' used

This is the updated code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
    int dag;
    int maand;
    int jaar;
}geboortedag;

typedef struct
{
    char name[BUFSIZ];
    geboortedag dag;
}persoon;

void readperson(FILE* fp, persoon *p);
geboortedag calculate(persoon *p);

int main(int argc, char *argv[])
{
    FILE *fp;
    persoon a;
    persoon *p;
    p = &a;
    geboortedag leeftijd;

    if (argc != 5)
    {
        printf("Het aantal argumenten die ingegeven is niet correct\n");
        getchar();
        exit(1);
    }
    else if (argc == 5)
    {
        if ((fp = fopen(argv[1], "r")) == NULL)
        {
            printf("De file kon niet worden geopend\n");
            getchar();
            exit(2);
        }
    }

    readperson(fp, p);
    leeftijd = calculate(p);
    printf("De leeftijd is %d %d %d", leeftijd.dag, leeftijd.maand, leeftijd.jaar);
    fclose(fp);
    printf("Het bestand is correct afegsloten");
    getchar();
    return 0;
}

void readperson(FILE* fp, persoon *x)
{
    char a[BUFSIZ];
    fgets(a, BUFSIZ, fp);
    strcpy(x->name, a);
    fscanf(fp, "%d%d%d", &x->dag.dag, &x->dag.maand, &x->dag.jaar);
}

geboortedag calculate(persoon *x)
{
    geboortedag tijd = { 0, 0, 0 };
    /*int a, b, c;
    a = atoi(x->dag.dag);
    b = atoi(x->dag.maand);
    c = atoi(x->dag.jaar);
    tijd.dag = x->dag.dag - a;
    tijd.maand = x->dag.maand;
    tijd.jaar = x->dag.jaar;*/
    return tijd;
}

The easiest way is to simply pass the file pointer to the read function:

void readperson(FILE *fp, persoon *x);

Then in main :

readperson(fp, p);

It is quite strange that it crashes when you declare fp globally but since you gave no more details, let's leave it.

Simply pass the file pointer to every procedure that needs it. So, their protos change into

void readperson(FILE *, persoon *);

and they are called as

readperson(fp, p);

from the main.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM