实验六 结构体与文件实验

一、 实验目的和要求
(1)掌握结构体类型以及结构体变量的定义与使用。
(2)综合运用结构体、数组、指针等知识,解决相关问题。
(3)会正确定义FILE*指针,掌握文件操作从打开、读写到关闭的完整过程。
(4)理解文本文件和二进制文件的区别和不同的读写方式。

二、实验环境(实验设备)
硬件: 微型计算
软件: Windows 操作系统、Microsoft Visual Studio 2010

三、实验原理及内容
实验题目(1)【见实验教材实验八的题目3】:编写程序exp8_3.c,验证用户输入的日期格式是否正确,如果不正确,则提示重新输入,直到重新输入正确为止。(提示:需要定义一个表示日期的结构体类型struct Date,包括年、月、日信息,并用typedef重新定义新类型名Date;检查日期是否有效,定义为函数int checkDate(Date date))。
实验解答:① 源程序代码如下:

#include <stdio.h>
struct Date
	{
		int year;
		int month;
		int day;
	}date;
	typedef struct Date Date;
int checkDate(Date date);
int main()
{
	int x;
	do
	{
		printf("Please input the date!\n");
		scanf("%d%d%d",&date.year,&date.month,&date.day);
		x=checkDate(date);
	}while(x==0);
	if(x)
		printf("Correct!\n");
	return 0;
}
int checkDate(Date date)
{
	int y=0;
	if(date.month==1||date.month==3||date.month==5||date.month==7||date.month==8||date.month==10||date.month==12)
		y=1;
	if(date.month==4||date.month==6||date.month==9||date.month==11)
		y=4;
	if(date.month==2)
	{
		if((date.year%4==0&&date.year%100!=0)||date.year%400==0)
			y=2;
		else
			y=3;
	}
	if(date.year<1900||date.year>2018)
		return 0;
	else if(date.month<1||date.month>12)
		return 0;
	else if(y==1&&(date.day<1||date.day>31))
		return 0;
	else if((y==4)&&(date.day<1||date.day>30))
		return 0;
	else if((y==2)&&(date.day<1||date.day>29))
		return 0;
	else if((y==3)&&(date.day<1||date.day>28))
		return 0;
	else
		return 1;
}

② 运行程序时,依次输入下面的几组年月日数据作为测试用例,观察程序的运行情况
测试用例 输入的原始数据 需要重新输入或你的输出结果
用例1 2002 4 31 Please input the date!
用例2 1809 12 3 Please input the date!
用例3 2020 5 4 Please input the date!
用例4 2000 2 29 Correct!
用例5 1908 14 23 Please input the date!
用例6 2003 11 -8 Please input the date!
用例7 1996 2 31 Please input the date!
用例8 1996 5 19 Correct!

实验题目(2)【见实验教材实验九的题目1】:编写程序exp9_1.c ,从键盘读入一系列字符并以“#”结束,将读入的字符(不包括#号)存入文本文件D:\f1.txt中,再从该文件读取内容,并在显示器上原样显示。
实验解答: 写出完整的源程序代码并做适当注释:

#include<stdio.h>
#include<stdlib.h>
void writefile(int ch,FILE *fp);
void readfile(int ch,FILE *fp);
int main()
{
	FILE *fp; //首先定义文件指针
	char ch=0;
	fp=fopen("D:\\f1.txt","w+");
	if(fp==0)                       //文件打开后需判断是否正确
	{
		printf("file error\n");
		exit(1);
	}
	writefile(ch,fp);
	rewind(fp);
	readfile(ch,fp);
	fclose(fp);
	return 0;
}
void writefile(int ch,FILE *fp)        //使用函数将键盘键入的字符写入文件,直到遇到字符#为止
{
	printf("Enter a text (end with '#'):\n");   
	ch=getchar();
	while(ch!='#')
	{
		fputc(ch,fp);
		ch=getchar();
	}
}
void readfile(int ch,FILE *fp)         //使用文件读取函数fgetc从该文件中读取字符,并显示出来
{
	while((ch=fgetc(fp))!=EOF)
	{
		putchar(ch);
	}
	putchar('\n');
}

实验题目(3)【见实验教材实验九的题目2】:某班有学生若干名(不超过40名),其信息的组织采用如下的结构体定义。编写程序exp9_2.c,完成要求的功能。
struct Student
{
char ID[20];
char name[30];
int age;
double score;
};
① 从键盘读入该班级学生的信息。
② 将所有的学生信息存入D:\Info.dat文件中、关闭该文件,建立文件定义函数CreateFile实现。
③ 另写一个函数ReadOut,将D:\Info.dat文件中的信息读入到内存,并依次输出到显示器上,该函数由main函数调用。
④编写函数Sort,实现按成绩由高到低将学生记录进行排序并输出排序后的结果。
⑤文件读写采用二进制读写(fread、fwrite)方式。实验解答: ①请写出完整的源程序代码并做适当注释:

#include<stdio.h>
#include<stdlib.h>
struct Student
{
	char ID[20];
	char name[30];
	int age;
	double score;
};
typedef struct Student Stu;
#define N 50
void CreateFile(Stu [],int n,FILE *fp);
void ReadOut(Stu [],int n,FILE *fp);
void Sort(Stu [],int len);
int main()
{
	int n,i,x;
	Stu stu[N];
	FILE *fp=NULL;                       //首先定义文件指针
	do
	{
		printf("Please input the number of students:\n");
	    scanf("%d",&n);
	}while(n<1||n>40);
	for(i=0;i<n;i++)
	{
		x=i+1;
		printf("%d(ID name age score):\n",x);
		scanf("%s%s%d%lf",stu[i].ID,stu[i].name,&stu[i].age,&stu[i].score);
	}
	CreateFile(stu,n,fp);
	printf("before being sorted:\n");
	ReadOut(stu,n,fp);
	printf("after being sorted:\n");
    Sort(stu,n);
	return 0;
}
void CreateFile(Stu stu[],int n,FILE *fp)
{
	fp=fopen("D:\\Info.dat","wb+");
	if(fp==0)                       //文件打开后需判断是否正确
	{
		printf("file error\n");
		exit(1);
	}
	fwrite(stu,sizeof(Stu),n,fp);
	fclose(fp);
}
void ReadOut(Stu stu[],int n,FILE *fp)
{
	int i=0;
	fp=fopen("D:\\Info.dat","rb");
	if(fp==0)                       //文件打开后需判断是否正确
	{
		printf("file error\n");
		exit(1);
	}
	fread(&stu[i],sizeof(Stu),n,fp);
	for(i=0;i<n;i++)
		printf("%s %s %d %.2f\n",stu[i].ID,stu[i].name,stu[i].age,stu[i].score);
	fclose(fp);
}
void Sort(Stu stu[],int len)
{
	int i,k,index;
	Stu temp;
	for(k=0;k<len-1;k++)
	{
		index=k;
		for(i=k+1;i<len;i++)
			if(stu[i].score>stu[index].score)
				index=i;
		if(index!=k)
		{
			temp=stu[index];
			stu[index]=stu[k];
			stu[k]=temp;
		}
	}
	for(i=0;i<len;i++)
		printf("%s %s %d %.2f\n",stu[i].ID,stu[i].name,stu[i].age,stu[i].score);
}

② 运行程序,你从键盘输入的内容以及屏幕显示的结果如下:

Please input the number of students:
3
1(ID name age score):
a A 1 10
2(ID name age score):
b B 2 11
3(ID name age score):
c C 3 7
before being sorted:
a A 1 10.00
b B 2 11.00
c C 3 7.00
after being sorted:
b B 2 11.00
a A 1 10.00
c C 3 7.00

四、实验小结(包括问题和解决方法、心得体会、意见与建议、实验出错信息及解决方案等)
(一)实验中遇到的主要问题及解决方法

1、 文件中路径中的斜杠应使用转义字符’\’
2、 结构体类型定义应写在Declarations后
3、 函数形参应写成数组类型,实参应写首地址
4、 注意一些格式,要牢记

(二)实验心得

编写程序还是要多上机,很多看似随意的知识,其实都是有玄机的。

(三)意见与建议(没有可省略)

五、支撑毕业要求指标点
1.2-H掌握计算机软硬件相关工程基础知识,能将其用于分析计算机及应用领域的相关工程问题。
3.1-M掌握设计/开发复杂工程问题解决方案所需要的专业知识和开发工具。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐