C语言文件191221

实验要求:

掌握c语言文件操作的基本方法
1.根据需求,选择不同的方式来打开文件
2.读取文件中的数据
3.向文件中写入数据
4.了解缓冲区以及EOF概念
5.了解一些基本的system语句:清屏,暂停等

任务一:

你现在拥有一个数组,数组中储存着总共10个人的姓名字符串
你需要为每个人创建一个txt文件,以他们的名字命名。
例如: 生成 笨笨.txt文件

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
    FILE *fp;
    int i;
    char name[10][20] = { "Ray","Haro","Scout","Iboy","Meiko","quanzhiyuan","chenwenlin","liruican","huxianzhao","tianye" };
    for (i = 0; i < 10; i++) {
        strcat(name[i], ".txt");//添加文本格式到数组里。 
        if ((fp = fopen(name[i], "w")) == NULL) {
            printf("File open error!\n");
            exit(0);
        }
        if (fclose(fp)) {
            printf("Close the file\n");
            exit(0);
        }
    }
    return 0;
}

任务二:

在任务一的基础上,这次不仅仅要创建txt文件,还需要往文件中写入
每个人的学号,性别,班级,线代成绩

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct ide {
    char name[10];
    char number[20];
    char gender[10];
    char classes[10];
    char score[10];
};
int main() {
    FILE *fp;
    int i;
    struct ide stu[10]; 
    char name[10][20] = { "Ray","Haro","Scout","Iboy","Meiko","全志愿","陈文林","李汭燦","胡显昭","田野" };
    for (i = 0; i < 10; i++) {
    	strcpy(stu[i].name,name[i]);
    	printf("请完善%s的个人信息:\n",name[i]);
		strcat(name[i], ".txt");//添加文本格式到数组里。 
		fp = fopen(name[i], "w");
		if(fp){
			printf("学号:"); 
			scanf("%s",stu[i].number);
			printf("性别:");
			scanf("%s",stu[i].gender);
			printf("班级:");
			scanf("%s",stu[i].classes);
			printf("线代成绩:");
			scanf("%s",stu[i].score);
			fprintf(fp,"%s %s %s %s %s\n",stu[i].name,stu[i].number,stu[i].gender,stu[i].classes,stu[i].score);
			if(fclose(fp)){
				printf("Can not close the file!\n");
				exit(0);
			}
			system("cls");//清屏。 
		} 
	}
    return 0;
}


任务三:

在任务二生成的文件中,将每个人的信息再重新读取出来,放入数组中。
[要求使用结构体数组,结构体需要包含姓名,学号,性别,班级,线代成绩5个属性]

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct ide {
    char name[10];
    char number[20];
    char gender[10];
    char classes[10];
    char score[10];
};
int main() {
    FILE *fp;
    int i;
    struct ide stu[10]; 
    char name[10][20] = { "Ray.txt","Haro.txt","Scout.txt","Iboy.txt","Meiko.txt","全志愿.txt","陈文林.txt","李汭燦.txt","胡显昭.txt","田野.txt" };
    for (i = 0; i < 10; i++) {
		fp = fopen(name[i], "r");
		if(fp)
		{
			fscanf(fp,"%s %s %s %s %s",stu[i].name,stu[i].number,stu[i].gender,stu[i].classes,stu[i].score);
			printf("%s\t%s\t%s\t%s\t%s\n",stu[i].name,stu[i].number,stu[i].gender,stu[i].classes,stu[i].score);
			fclose(fp);
			system("pause");//暂停。
		}
	}
	return 0;
}

任务四:

试着使用一下system("cls"),system("pause")这两个命令,看看这两个
命令能不能对你的程序起到一些美化的作用。
拓展:上网搜索类似的其他函数,使用并解释他们的作用。
【想找几个是几个】

运用:
system("cls");见任务二倒数第5行。
system("pause");见任务三倒数第5行。
拓展:
简单函数如:system("color XX");
颜色属性由两个十六进制数字指定 – 第一个为背景,第二个则为前景。每个数字可以为以下任何值之一:

0 = 黑色       8 = 灰色
1 = 蓝色       9 = 淡蓝色
2 = 绿色       A = 淡绿色
3 = 湖蓝色     B = 淡浅绿色
4 = 红色       C = 淡红色
5 = 紫色       D = 淡紫色
6 = 黄色       E = 淡黄色
7 = 白色       F = 亮白色

常用的DOS命令,都可以用system函数调用:

MORE     一次显示一个结果屏幕。
DATE     显示或设置日期。
SORT     对输入进行分类。
TIME     显示或设置系统时间。

posted @ 2019-12-21 20:52  蛋炒饭特稀  阅读(135)  评论(0编辑  收藏  举报