一、实验目的
1.文件和文件指针的概念以及文件的定义方法。
2.了解文件打开和关闭的概念及方法。
3.掌握有关文件的函数。
二、实验学时
2 学时
三、实验内容
1.对 data.dat 文件写入 100 条记录。
2.设有一文件 cj.dat 存放了 50 个人的成绩(英语、计算机、数学),存放格式
为:每人一行,成绩间由逗号分隔。计算三门课平均成绩,统计个人平均成绩大
于或等于 90 分的学生人数。
3.统计上题 cj.dat 文件中每个学生的总成绩,并将原有数据和计算出的总分数
存放在磁盘文件“stud”中。
补充:
dev编译时遇到 [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
(主要原因是指令没有引入c++11支持引入)
解决方法如下:
首先确保Dev C++版本是最新的5.11版其次 打开最上角菜单栏——工具——编译选项在编译时加入以下命令方括号里打钩, 然后在下面文本框里输入-std=c++11就行了.
注:-std=c++11,支持C++11标准;
-std=gnu++11,支持C++11标准和GNU扩展特性
①由文件指针打开文件进行读写
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct student
{
int Chinese ;
int English ;
int math ;
int computer ;
} s ;
main(void)
{
FILE *fp ;
s a[100] ;
srand(time(NULL)) ;
if ((fp = fopen("data.dat", "a")) == NULL)
{
printf("Failure to open data.dat!\n") ;
exit(0) ;
}
for (int i = 0 ; i < 100 ; i++)
{
a[i] = {rand()%100, rand()%100, rand()%100, rand()%100} ;
}
for (int i = 0 ; i < 100 ; i++)
fprintf(fp, "%5d%5d%5d%5d\n", a[i].Chinese, a[i].computer, a[i].English, a[i].math) ;
fclose(fp) ;
}
②通过重定位语句freopen,改变输入的来源和输出的目的地来进行文件读写
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct student
{
int Chinese ;
int English ;
int math ;
int computer ;
} s ;
main(void)
{
s a[100] ;
srand(time(NULL)) ;
freopen("data2.dat","w",stdout) ;
for (int i = 0 ; i < 100 ; i++)
a[i] = {rand()%100, rand()%100, rand()%100, rand()%100} ;
for (int i = 0 ; i < 100 ; i++)
printf("%5d%5d%5d%5d\n", a[i].Chinese, a[i].computer, a[i].English, a[i].math) ;
freopen("CON","w",stdout) ;
}
2.通过文件指针
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct grade
{
int number ;
int English ;
int computer ;
int math ;
} g ;
void WritetoFile(g cj[]) ;
void ReadfromFile(g cj[]) ;
void stats(g cj[]) ;
main(void)
{
g a[50], b[50] ;
srand(time(NULL)) ;
for (int i = 0 ; i < 50 ; i++)
{
a[i] = {i+1, rand()%120+1, rand()%120+1, rand()%120+1} ;
}
WritetoFile(a) ;
ReadfromFile(b) ;
stats(b) ;
}
void WritetoFile(g cj[])
{
FILE *fp ;
if ((fp = fopen("cj.dat", "w")) == NULL)
{
printf("Failure to open cj.dat ! \n") ;
exit(0) ;
}
for (int i = 0 ; i < 50 ; i++)
{
fprintf(fp, "%-7d%d,%d,%d\n", cj[i].number, cj[i].English, cj[i].computer, cj[i].math) ;
}
fclose(fp) ;
}
void ReadfromFile(g cj[])
{
FILE *fp ;
if ((fp = fopen("cj.dat", "r")) == NULL)
{
printf("Failure to open cj.dat ! \n") ;
exit(0) ;
}
for (int i = 0 ; i < 50 ; i++)
{
fscanf(fp, "%d%d,%d,%d ", &cj[i].number, &cj[i].English, &cj[i].computer, &cj[i].math) ;
}
fclose(fp) ;
}
void stats(g cj[])
{
double aver ; int j = 0 ;
for (int i = 0 ; i < 50 ; i++)
{
aver = (double)(cj[i].English + cj[i].computer + cj[i].math) / 3 ;
if (aver >= 90)
j++ ;
printf("%d %lf\n",i+1,aver);
}
printf("%d", j) ;
}
②通过重定位语句freopen,修改读写两个函数
void WritetoFile(g cj[])
{
freopen("cj2.dat", "w", stdout) ;
for (int i = 0 ; i < 50 ; i++)
printf("%-7d%d,%d,%d\n", cj[i].number, cj[i].English, cj[i].computer, cj[i].math) ;
freopen("CON","w",stdout) ;
}
void ReadfromFile(g cj[])
{
freopen("cj2.dat", "r", stdin) ;
for (int i = 0 ; i < 50 ; i++)
scanf("%d%d,%d,%d ", &cj[i].number, &cj[i].English, &cj[i].computer, &cj[i].math) ;
freopen("CON","r",stdin) ;
}
①通过文件指针
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct grade
{
int number ;
int English ;
int computer ;
int math ;
} g ;
void WritetoFile(g cj[], int total[]) ;
void ReadfromFile(g cj[]) ;
void stats(g cj[], int total[]) ;
main(void)
{
g b[50] ;
int total[50] ;
ReadfromFile(b) ;
stats(b, total) ;
WritetoFile(b, total) ;
}
void WritetoFile(g cj[], int total[])
{
FILE *fp ;
if ((fp = fopen("D://stud.txt", "w")) == NULL)
{
printf("Failure to open stud.txt ! \n") ;
exit(0) ;
}
for (int i = 0 ; i < 50 ; i++)
{
fprintf(fp, "%-7d%d,%d,%d %d\n", cj[i].number, cj[i].English, cj[i].computer, cj[i].math, total[i]) ;
}
fclose(fp) ;
}
void ReadfromFile(g cj[])
{
FILE *fp ;
if ((fp = fopen("cj.dat", "r")) == NULL)
{
printf("Failure to open cj.dat ! \n") ;
exit(0) ;
}
for (int i = 0 ; i < 50 ; i++)
{
fscanf(fp, "%d%d,%d,%d ", &cj[i].number, &cj[i].English, &cj[i].computer, &cj[i].math) ;
}
fclose(fp) ;
}
void stats(g cj[], int total[])
{
for (int i = 0 ; i < 50 ; i++)
{
total[i] = cj[i].English + cj[i].computer + cj[i].math ;
printf("%d\n", total[i]);
}
}
②通过重定位语句freopen,同上