第七次课内实验
1.以只写的方式打开一个文件stu.txt,从键盘输入一组字符,将其保存到磁盘文件stu.txt中。
#include <stdio.h>
#include <stdlib.h>
//以只写的方式打开一个文件stu.txt,从键盘输入一组字符,将其保存到磁盘文件stu.txt中。
void main()
{
FILE *fp;
char c;
if((fp=fopen("stu.txt","w"))==NULL)//以只写的方式打开一个文件stu.txt
{
printf("cannot open this file\n");
exit(0);
}
c=getchar();//从键盘输入一个字符
while(c!='#') //以#作为结尾标识符
{
putchar(c);
fputc(c,fp);//保存到磁盘文件stu.txt中
c=getchar();//从键盘继续输入一个字符
}
fclose(fp);//关闭文件
}
2.以只读方式打开刚才的文件stu.txt,读取文件中的字符,保存到字符数组str1中,并输出到屏幕上。
#include <stdio.h>
#include <stdlib.h>
//以只读方式打开刚才的文件stu.txt,读取文件中的字符,保存到字符数组str1中,并输出到屏幕上。
void main()
{
FILE *fp;
char c;
char str1[100];
if((fp=fopen("stu.txt","r"))==NULL)//以只读方式打开刚才的文件stu.txt
{
printf("cannot open this file\n");
exit(0);
}
fgets(str1,100,fp);//读取文件中的字符并保存到字符数组str1中
printf("%s",str1);//输出到屏幕上
printf("\n");
fclose(fp);//关闭文件
}
3.有6个学生,每个学生数据包括学号、姓名、3门课程的成绩。定义如下结构体数组存放这6个学生的数据:
struct student
{
int num;
char name[20];
int score[3];
}stu[6];
编写程序实现如下功能:
(1)从键盘输入6个学生的数据,将这6个学生的数据以ASCII格式写入名为stu.txt的文本文件中。
(2)从stu.txt中读取数据到结构体数组stu1中,并输出stu1中的数据。
#include <stdio.h>
#include <stdlib.h>
//从键盘输入6个学生的数据,将这6个学生的数据以ASCII格式写入名为stu.txt的文本文件中。
//从stu.txt中读取数据到结构体数组stu1中,并输出stu1中的数据。
struct student //创建stu和stu1结构体
{
int num;
char name[20];
int score[3];
}stu[6],stu1[6];
void main()
{
void input(struct student stu[],int n);
void output(struct student stu[],int n);
void read_in(struct student stu[],int n);
void save(struct student stu[],int n);
int n=6;
input(stu,n);
read_in(stu,n);
output(stu,n);
}
void save(struct student stu[],int n)//将键盘输入6个学生的数据保存
{
FILE *fp;
int i;
if((fp=fopen("stu.txt","w"))==NULL)//新建打开stu.txt文件
{
printf("cannot open this file\n");
exit(0);
}
for(i=0;i<n;i++)//将输入的stu[]以ASCII格式数据存入stu.txt文件中
{
fprintf(fp,"%d %s %d %d %d\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
fclose(fp);//关闭文件
}
void input(struct student stu[],int n)//从键盘输入6个学生的数据
{
int i;
printf("请输入6个学生的学号、姓名和三门课的成绩\n");
for(i=0;i<n;i++)//利用for循环开始输入中的6个学生的数据
{
scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
save(stu,n);//将键盘输入6个学生的数据保存
}
void read_in(struct student stu[],int n) //从stu.txt中读取数据到结构体数组stu1中,并输出stu1中的数据。
{
FILE *fp;
int i=0;
if((fp=fopen("stu.txt","r"))==NULL)//以只读方式打开stu.txt文件
{
printf("cannot open this file\n");
exit(0);
}
for(i=0;i<n;i++)//利用for循环和fscanf函数将数据存入stu1数组
{
fscanf(fp,"%d%s%d%d%d\n",&stu1[i].num,stu1[i].name,&stu1[i].score[0],&stu1[i].score[1],&stu1[i].score[2]);
}
fclose(fp); //关闭文件
}
void output(struct student stu[],int n)//输出stu1中的数据
{
int i;
printf("输出6个学生的学号、姓名和三门课的成绩\n");
for(i=0;i<n;i++)//利用for循环依次输出stu1中的数据
{
printf("%d %s %d %d %d\n",stu1[i].num,stu1[i].name,stu1[i].score[0],stu1[i].score[1],stu1[i].score[2]);
}
}
4.将第三题的数据,以二进制的格式写入到stu.dat的二进制文件中,同时将数据从文件中读取,并输出至屏幕。
#include <stdio.h>
#include <stdlib.h>
//将第三题的数据以二进制的格式写入到stu.dat的二进制文件中,同时将数据从文件中读取,并输出至屏幕。
struct student //创建stu结构体
{
int num;
char name[20];
int score[3];
}stu[6];
void main()
{
void input(struct student stu[],int n);
void output(struct student stu[],int n);
void read_in(struct student stu[],int n);
int n=6;
input(stu,n);
read_in(stu,n);
output(stu,n);
}
void save(struct student stu[],int n)//将键盘输入6个学生的数据保存
{
FILE *fp;
int i;
if((fp=fopen("stu.dat","w"))==NULL)//新建打开stu.dat文件
{
printf("cannot open this file\n");
exit(0);
}
for(i=0;i<n;i++)//将输入的stu[]以二进制格式数据存入stu.dat文件中
{
fwrite(&stu[i],sizeof(struct student),1,fp);
}
fclose(fp);//关闭文件
}
void input(struct student stu[],int n)//从键盘输入6个学生的数据
{
int i;
printf("请输入6个学生的学号、姓名和三门课的成绩\n");
for(i=0;i<n;i++)//利用for循环开始输入中的6个学生的数据
{
scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
save(stu,n);//将键盘输入6个学生的数据保存
}
void read_in(struct student stu[],int n) //从stu.txt中读取数据到结构体数组stu中
{
FILE *fp;
int i=0;
if((fp=fopen("stu.dat","r"))==NULL)//以只读方式打开stu.txt文件
{
printf("cannot open this file\n");
exit(0);
}
for(i=0;i<n;i++)//利用for循环和fread函数将数据存入stu1数组
{
fread(&stu[i],sizeof(struct student),1,fp);
}
fclose(fp); //关闭文件
}
void output(struct student stu[],int n)//输出stu中的数据
{
int i;
printf("输出6个学生的学号、姓名和三门课的成绩\n");
for(i=0;i<n;i++)//利用for循环依次输出stu1中的数据
{
printf("%d %s %d %d %d\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
}
5.选做题:从第三题的文本文件中读取第1、3、5名学生的数据,将其存入结构体数组stu1中,并输出至屏幕。
#include <stdio.h>
#include <stdlib.h>
//选做题:从第三题的文本文件中读取第1、3、5名学生的数据,将其存入结构体数组stu1中,并输出至屏幕。
struct student //创建stu1结构体
{
int num;
char name[20];
int score[3];
}stu1[6];
void main()
{
int i,n=6;
FILE *fp;
if((fp=fopen("stu.txt","r"))==NULL)//以只读方式打开stu.txt文件
{
printf("can not open file\n");
exit(0);
}
for(i=0;i<3;i++)//利用for循环和fscanf函数将数据存入stu1数组
{
fscanf(fp,"%d%s%d%d%d",&stu1[i].num,&stu1[i].name,&stu1[i].score[0],&stu1[i].score[1],&stu1[i].score[2]);
fseek(fp,sizeof(struct student)/2,1);//通过fseek函数将文件位置标记向前移动到离当前位置一个结构体长度的位置
}
for(i=0;i<3;i++)//利用for循环依次输出结构题stu1数组
{
printf("%d %s %d %d %d\n",stu1[i].num,stu1[i].name,stu1[i].score[0],stu1[i].score[1],stu1[i].score[2]);
}
fclose(fp);//关闭文件
}