简体   繁体   English

C,将文件的指针作为参数传递给aa函数

[英]C, passing a pointer for a file into a a function as an argument

I am making a function to sort data in a txt file in order to produce the min, mean, and max of the data within. 我正在制作一个函数来对txt文件中的数据进行排序,以生成其中的数据的最小值,平均值和最大值。

I want to re-use the function on multiple files. 我想在多个文件上重复使用该功能。 Therefore, I'd like to pass the pointers for files into the function as an argument. 因此,我想将文件的指针作为参数传递给函数。 However, my compiler gives me the following error: 但是,我的编译器给我以下错误:

LA1.c:19:10: error: unknown type name 'FILE' void mmm(FILE *ifile, int *i, int *min, int *max, float *mu); LA1.c:19:10:错误:未知类型名称'FILE'void mmm(FILE * ifile,int * i,int * min,int * max,float * mu);

I've attached my code below. 我在下面附加了我的代码。 Please exclude some of the extraneous variables as I anticipated using those later but have not finished the majority of this program. 请排除一些多余的变量,因为我以后会使用它们,但是尚未完成该程序的大部分操作。 Any help is appreciated. 任何帮助表示赞赏。

Thanks in advance. 提前致谢。

/* Purpose: (1) Sort Data from 5 different years (2) Calculate Standard Deviation */
/* (3) find max & min from each set (4) calculate deviation standard deviation  */
/*  units, AKA σ (5) convert to letter grade */
/*************************************************************************/


/* Outline: (A) funct: scan data ifile (main) calc mean (μ), max, min  */
/* (B) funct: rescan data and calc sigma (C) main: Rescan data, calc Δ μετα' σ and letter */   
/* grade each for each datum.  (D) Print to minitor in main.  Use dow while?loop for rows*/
/*************************************************************************/

/*prototypes*/

void mmm(FILE *ifile, int *i, int *min, int *max, float *mu);   
/* mmm stands for min, max, mean */

float sigma (int *num, float *sigma);       /*calculates sigma(standard deviation) */

/* libraries */

#include <stdio.h>
#include <math.h>

void main (void) {

/* main variable declarations max of i vars used for rows loop counter in main */


float sigma, mu;
int i, i09, i10, i11, i12, i13, min, max, f09max, f09min, f10max, f10min, f11max, 
f11min, f12max, f12min, f13max, f13min;     

FILE *ifile;

ifile=fopen("2009.txt", "r");


mmm(&ifile, &i, &min, &max, &mu);

fclose(ifile);  /* file must be closed to rescan in other functions */

}

void mmm(FILE *ifile, int *i, int *min, int *max, float *mu) {

printf("running function mean \n");

int sum, num;

*max=0; /* Score range is between 0 and 100 in a class, thus use of fixed initials */
*min=100;
*i=0;
num=0;
sum=0;

    while (fscanf(ifile, "%i", &num) !=EOF)  {

    printf("Variable num is %i. \n", num);

    fscanf(ifile, "%d", &num);

        if (num>*max) {*max=num;} /* assigns global max and min */
        if (num<*min) {*min=num;} /* to be stored to different years in funct main*/

    *i=*i+1;

    sum= sum + num; /* sums for average calculation */

    }

*mu= ((float)sum/(*i));

printf("The min is %i.  The max is %i.\n", *min, *max);
printf("i is %i, sum is %i, and mu is %f \n", *i, sum, *mu);

}

You're declaring your function before the #include <stdio.h> line, so the compiler hasn't seen the definition of the FILE type yet. 您在#include <stdio.h>行之前声明了函数,因此编译器尚未看到FILE类型的定义。 Just move your #include lines up to the top of the file. 只需将#include行移动到文件顶部即可。

In C the declarations of everything needs to be before they are used. 在C语言中,必须先声明所有内容,然后再使用它们。 Since you include the header file where FILE is declared after the function prototype where FILE is used, the compiler will not know what FILE is because by that point (at the function declaration) it haven't seen that declaration. 既然你包括在头文件FILE的函数原型,其中声明FILE时,编译器将不知道是什么FILE ,因为到那个时候(在函数声明),它没有看到该声明。

The '#include' statements in C are preprocessor directives. C中的“ #include”语句是预处理程序指令。 The preprocessor is a program invoked by the C compiler on your source code that follows the commands given in these directives and does text manipulation on the files before compilation. 预处理程序是C编译器在您的源代码上调用的程序,它遵循这些指令中给出的命令,并在编译之前对文件进行文本处理。 When it sees the `#include' statements, it grabs those files and inserts them into your code. 看到“ #include”语句时,它将抓取这些文件并将其插入您的代码中。 By putting those directives after your function prototypes, you are making all types defined in those other source files only visible to the portion of your program following those directives. 通过将这些指令放在函数原型之后,可以使其他源文件中定义的所有类型仅对那些指令之后的程序部分可见。 So, basically, put preprocessor directives like this at the very top of your program to make them usable by all of your code. 因此,基本上,将这样的预处理器指令放在程序的顶部,以使它们可用于所有代码。

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

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