简体   繁体   English

如何在C中的调用函数中使用文件指针

[英]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. 我用fopen在main函数中初始化了我的filepointer,问题是我想在main调用的函数中使用相同的指针。 My program compiles when i declare my FILE *fp outside the main function but it crashes while running. 当我在主函数之外声明FILE * fp时,程序会编译,但运行时会崩溃。 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 错误C4703可能使用了未初始化的本地指针变量'fp'

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: 最简单的方法是将文件指针简单地传递给read函数:

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

Then in main : 然后在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. 奇怪的是,当您全局声明fp时,它会崩溃,但是由于您没有提供更多详细信息,因此我们将其保留。

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. 从主要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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