C语言文件

C语言文件

一.代码展示

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ROW 26
#define COL 74
void show();//定义运行框的大小和背景颜色
void printhome();//显示首页
void printend();//显示尾页

int i, j,COL2=74, flag=1;
typedef struct {
	char num[14];
	char name[20];
	char sex[3];
	char Class[10];
	char score[4];
}STU;
int main()
{
	char name1[10][20];//用来指定生成名字文件
	char name2[10][20];//储存名字
	int i;
	FILE* fp;
	STU stu1[10],stu2[10];//stu1用来写入文件,stu2用来读取文件
	show();
	printhome();
	printf("\n\n\n\n");
	printf("                               欢迎使用!\n");
	printf("                              请进入系统!\n");
	printf("\n\n\n\n");
	printf("                            ");
	system("pause");//暂停
	for (i = 0; i < 10; i++)
	{
		printhome();
		printf("\n\n");
		printf("               %d.\n", i + 1);
		printf("               请输入姓名:\n");
		printf("               ");
		gets_s(name1[i]);
		strcpy(name2[i], name1[i]);
		strcat(name1[i], ".txt");//生成文件后缀名
		strcpy(stu1[i].name, name1[i]);
		fp = fopen(name1[i], "w");
		if (fp== NULL)
		{
			printf("File open error!\n");
			exit(0);
		}
		printf("               请输入学号:   性别:   班级:     线代成绩:\n");
		printf("               ");
		scanf("%s  %s      %s%s", stu1[i].num, stu1[i].sex, stu1[i].Class, stu1[i].score);
		fprintf(fp, "%s    %s    %s    %s\n", stu1[i].num, stu1[i].sex, stu1[i].Class, stu1[i].score);
		if (fclose(fp))
		{
			printf("Can not close the file!\n");
			exit(0);
		}
		getchar();
		system("cls");//清空界面
		
	}
	printhome();
	printf("\n\n\n\n");
	printf("                        下面查看所录入的学生信息!\n");
	printf("\n\n\n\n");
	printf("                            ");
	system("pause");
	system("cls");
	printhome();
	printf("             姓名:    学号:         性别:   班级:     线代成绩:\n");
	for (i = 0; i < 10; i++)
	{
		if ((fp = fopen(name1[i], "r")) == NULL)
		{
			printf("File open error!\n");
			exit(0);
		}
		strcpy(stu2[i].name, name2[i]);
		fscanf(fp, "%s %s %s %s\n", stu2[i].num, stu2[i].sex, stu2[i].Class, stu2[i].score);
		if (fclose(fp))
		{
			printf("Can not close the file!\n");
			exit(0);
		}
		printf("             ");
		printf("%-10s", stu2[i].name);
		printf("%-15s", stu2[i].num);
		printf("%-9s", stu2[i].sex);
		printf("%-11s", stu2[i].Class);
		printf("%-4s\n", stu2[i].score);

	}
	printf("\n\n\n\n");
	printf("                     请输入0退出系统!\n");
	printf("                     ");
	scanf("%d", &flag);
	if (flag == 0)//用来退出系统
    {
		printend();
	}
	return 0;
}
void show()//定义运行框的大小和背景颜色
{
	system("mode con cols=79 lines=34");//DOS窗口的尺寸会变为34行79列
	system("color F4");//F指改变背景为白色,4指改变字体颜色为红色。
}
void printhome()//显示首页
{
	system("cls");
	printf("┏");
	for (j = 0; j < COL2; j++)
	{
		printf("━");
	}
	printf("┓\n");
	printf("┃  ★☆★☆★☆★☆★☆★ 欢迎使用学生信息管理系统 ★☆★☆★☆★☆★☆★  ┃\n");
	printf("┣");
	for (j = 0; j < COL2; j++)
	{
		printf("━");
	}
	printf("┫\n");
	
	printf("┗");
	
	for (j = 0; j < COL2; j++)
	{
		printf("━");
	}
	printf("┛\n");
}
void printend()//显示尾页 
{
	system("cls");
	printf("┏");
	for (j = 0; j < COL2; j++)
	{
		printf("━");
	}
	printf("┓\n");
	printf("┃  ★☆★☆★☆★☆★☆★ 欢迎使用学生信息管理系统 ★☆★☆★☆★☆★☆★  ┃\n");
	printf("┣");
	for (j = 0; j < COL2; j++)
	{
		printf("━");
	}
	printf("┫\n");
	for (i = 0; i < ROW; i++)
	{
		printf("┃");
		if (i == ROW / 2)
		{
			printf("                               感谢您的使用!                             ");
		}
		else
		{
			for (j = 0; j < COL; j++)
			{
				printf(" ");
			}
		}
		printf("┃\n");
	}
	printf("┣");
	for (j = 0; j < COL2; j++)
	{
		printf("━");
	}
	printf("┫\n");
	printf("┃                               成功退出系统。                             ┃\n");
	printf("┗");
	for (j = 0; j < COL2; j++)
	{
		printf("━");
	}
	printf("┛\n");
}

二.运行展示





自动生成文件,每个文件中存放着每个人的学号,性别,班级,线代成绩。

三.system()

system("pause")可以实现冻结屏幕,便于观察程序的执行结果。

system("CLS")可以实现清屏操作。

system("color 0A"); 其中color后面的0是背景色代号,A是前景色代号。

0=黑色 8=灰色

1 = 蓝色 9 = 淡蓝色

2 = 绿色 A = 淡绿色

3 = 浅绿色 B = 淡浅绿色

4 = 红色 C = 淡红色

5 = 紫色 D = 淡紫色

6 = 黄色 E = 淡黄色

7 = 白色 F = 亮白色

system("mode con cols=48 lines=25")作用是定义运行框的大小,DOS窗口的尺寸会变为25行48列。

四.总结

1.通过书本和别人的帮助,学会了文件的基本操作,为之后的课设打下基础。
2.运用system()来美化运行界面。
3.利用百度搜索自己想要的界面美化,通过一步步的运行,调整,最终得到自己想要的界面。在接下来的课设中,界面美化是加分项,需要让用户有良好的操作体验。
4.了解缓冲区以及EOF概念,虽然本次没用到二进制文件,但是对它有一定的了解。
posted @ 2019-12-15 20:28  拒绝平庸*  阅读(535)  评论(0编辑  收藏  举报