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

标签 c file function pointers

我正在制作一个函数来对 txt 文件中的数据进行排序,以便生成其中数据的最小值、平均值和最大值。

我想在多个文件上重复使用该功能。因此,我想将文件指针作为参数传递给函数。但是,我的编译器给我以下错误:

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

我在下面附上了我的代码。请排除一些无关变量,因为我预计稍后会使用这些变量,但尚未完成该程序的大部分内容。感谢您的帮助。

提前致谢。

/* 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);

}

最佳答案

您要在 #include <stdio.h> 之前声明您的函数行,所以编译器没有看到 FILE 的定义打字呢。只需移动您的 #include排到文件的顶部。

关于C,将文件指针作为参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26326375/

相关文章:

c - MySQL Connector for C- 连接到远程数据库

c - 从命令行参数分配数组

MySQL 使用 while 循环函数插入多行

c - 如何使用 FILE 作为 C 函数的参数?

javascript - JavaScript 中的 Require() 函数

无法用c中的for循环写入文本文件

c++ - 如何以图形方式显示 .map 文件中的内存布局?

Java将文本添加到文件中的特定行

android - 在 Android 中通过 Intent 限制可选择的文件类型

Java - 一个文件路径是另一个文件路径的一部分吗?