C语言中文件操作为什么在函数调用中读不了文件内容?

不知道为什么在select函数中读不了文件的内容,也不知道程序哪里错了,救救孩子吧,孩子已经改了一天都找不到错误在哪里,如果能帮忙指点,一定会积极采纳!谢谢啦!!!

img

img

img

img

-

修改如下,供参考:

#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
struct  huanjingcanshu {
    int   month;
    int   date;
    float temperature;
    float humidity;
    float lux;
    float ph;
};
double select(FILE* f2,int month) 
{
     
    //rewind(f2); 
    struct huanjingcanshu b[3]; 
    int i,j=0; 
    double aver = 0;
    for(i=0;i<3;i++) 
    { 
        fread(&b[i], sizeof(struct huanjingcanshu), 1, f2);
        printf("%d %d %f %f %f %f\n",b[i].month,b[i].date,b[i].temperature,b[i].humidity,b[i].lux,b[i].ph); 
        if(b[i].month==month) 
        {
            aver += b[i].temperature;
            j++;
        } 
    } 
    if (j != 0)
        aver=aver/j;
    //fclose(f2); 
    return aver; 
} 
void input_data()
{
    int i;
    struct huanjingcanshu a[3];
    FILE* fp = fopen("d:\\record.dat", "wb");
    if (NULL == fp)
    {
        printf("打开失败\n");
        exit(0);
    }
    printf("请输入具体数值\n");
    for (i = 0; i < 3; i++)
    {
        scanf("%d %d %f %f %f %f", &a[i].month, &a[i].date, &a[i].temperature, &a[i].humidity, &a[i].lux, &a[i].ph);
        fwrite(&a[i], sizeof(struct huanjingcanshu), 1, fp);
    }
    fclose(fp);
}
int main() 
{ 
    double aver;
    char   ch;
    int    i, mon, jj = 0;
    FILE* fp;
    while(1)
    { 
        printf("初始化:s  查询:g  退出:q\n"); 
        scanf(" %c", &ch);
        getchar();
        if (ch == 'q') {

            break;
            //exit(0);
        } 
        else if(ch=='g'){
            printf("请输入月份\n");
            scanf("%d", &mon);
            fp = fopen("d:\\record.dat", "rb");
            if (NULL == fp)
            {
                printf("打开失败\n");
                break;              //exit(0);
            }
            aver = select(fp, mon);
            fclose(fp);
            printf("月平均气温为:%lf\n", aver);
        }
        else if (ch == 's') {
            input_data();
        }
    } 
    return 0; 
}

可否将代码贴出来

首先验证下写文件有没有问题,可以写过之后将文件close然后用记事本打开看看有没有内容,如果有,在验证是否读文件有问题