不知道为什么在select函数中读不了文件的内容,也不知道程序哪里错了,救救孩子吧,孩子已经改了一天都找不到错误在哪里,如果能帮忙指点,一定会积极采纳!谢谢啦!!!
-
不知道为什么在select函数中读不了文件的内容,也不知道程序哪里错了,救救孩子吧,孩子已经改了一天都找不到错误在哪里,如果能帮忙指点,一定会积极采纳!谢谢啦!!!
-
修改如下,供参考:
#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;
}