文件初学

文件初学

1. 让我们做任务一(system的相关用法在注释中,不单独列出)

FILE* fp;
	char name[10][100] = { "黄","林","万","张","王","何","李","吴","商","潘" };
	char name2[10][100] = { "黄","林","万","张","王","何","李","吴","商","潘" };//因为name字符数组被改变
	for (int i = 0; i < 10; i++)
	{
		sprintf(name[i], "%s.txt", name[i]);//以二维字符数组里的元素作为文件名,使其变为name[i].txt
		fp = fopen(name[i], "w");//利用fopen函数用“w"方式写,创建新文件
		if (fp == NULL) {//安全检查,文件可能打开失败
			break;
		}
		if (fclose(fp)) {
			printf("Can't close the file!\n");
			exit(0);
		}
	}
	system("dir");//读取目录中文件名
	system("pause");
	system("cls");

2. 让我们做任务二

使用fputs(写fwrite比较长,就不搞上去了)

char num[20][20];
	char class3_4[10][10];
	char sex[10][10];
	char linear_algebra_score[10][10];//定义数据
	printf("Please enter num class sex linear_algebra_score\n");
	system("color 1a");
	for (int i = 0; i < 10; i++) {
		if ((fp = fopen(name[i], "w+")) == NULL) {//打开文件
			printf("File open error!\n");
			exit(0);//退出程序,关闭流,防止异常
		}
		scanf("%s %s %s %s", num[i], class3_4[i], sex[i], linear_algebra_score[i]);//键盘输入数据
		//fprintf(fp, "%s %s %s %s", num[i], class3_4[i], sex[i], linear_algebra_score[i]);//文件读入数据
		fputs(num[i], fp);//文件读入字符串
		fprintf(fp, " ");
		fputs(class3_4[i], fp);
		fprintf(fp, " ");
		fputs(sex[i], fp);
		fprintf(fp, " ");
		fputs(linear_algebra_score[i], fp);
		if (fclose(fp)) {
			printf("Can't close the file!\n");
			exit(0);
		}
	}
	system("pause");//暂停
	system("cls");//清屏

使用fprintf

char num[20][20];
	char class3_4[10][10];
	char sex[10][10];
	char linear_algebra_score[10][10];//定义数据
	printf("Please enter num class sex linear_algebra_score\n");
	system("color 1a");
	for (int i = 0; i < 10; i++) {
		if ((fp = fopen(name[i], "w+")) == NULL) {//打开文件
			printf("File open error!\n");
			exit(0);//退出程序,关闭流,防止异常
		}
		scanf("%s %s %s %s", num[i], class3_4[i], sex[i], linear_algebra_score[i]);//键盘输入数据
		fprintf(fp, "%s %s %s %s", num[i], class3_4[i], sex[i], linear_algebra_score[i]);//文件读入数据
		if (fclose(fp)) {
			printf("Can't close the file!\n");
			exit(0);
		}
	}


3.让我们做任务三

for (int i = 0; i < 10; i++) {
		if ((fp = fopen(name[i], "r")) == NULL) {
			printf("File open error");
			exit(0);
		}
		while (!feof(fp)) {//看文件是否读到尾,防止出错
			fscanf(fp, "%s %s %s %s", student1[i].num1, student1[i].class3_41, student1[i].sex1, student1[i].linear_algebra_score1);
		}
		if (fclose(fp)) {
			printf("Can't close the file!\n");
			exit(0);
		}
	}
	system("pause");
	system("cls");
	system("color 0a");//颜色函数
	printf("you will find name num class sex linear_algebra_score\n");
	for (int i = 0; i < 10; i++) {
		strcpy(student1[i].name1, name2[i]);//将原有的的姓名数组复制到结构体的姓名数组
		printf("%s\t%s\t%s\t%s\t%s\n",student1[i].name1, student1[i].num1, student1[i].class3_41, student1[i].sex1, student1[i].linear_algebra_score1);
	    //检验数据是否读入结构体
	}
	//system("pause");
	Sleep(2500);//windows头文件里的函数,程序休眠2500秒

4.任务四在代码中

5.附完整代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct student {
	char name1[10];
	char num1[20];
	char class3_41[10];
	char sex1[10];
	char linear_algebra_score1[10];
}student1[15];//定义结构体,通过student1调用
int  main() {
	FILE* fp;
	char name[10][100] = { "黄","林","万","张","王","何","李","吴","商","潘" };
	char name2[10][100] = { "黄","林","万","张","王","何","李","吴","商","潘" };//因为name字符数组被改变
	for (int i = 0; i < 10; i++)
	{
		sprintf(name[i], "%s.txt", name[i]);//以二维字符数组里的元素作为文件名,使其变为name[i].txt
		fp = fopen(name[i], "w");//利用fopen函数用“w"方式写,创建新文件
		if (fp == NULL) {//安全检查,文件可能打开失败
			break;
		}
		if (fclose(fp)) {
			printf("Can't close the file!\n");
			exit(0);
		}
	}
	system("dir");//读取目录中文件名
	system("pause");
	system("cls");
	char num[20][20];
	char class3_4[10][10];
	char sex[10][10];
	char linear_algebra_score[10][10];//定义数据
	//printf("       num      class    sex   linear_algebra_score\n");
	system("color 0a");
	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n");
	printf("          ||**         ******       **                   **        ******            **||\n");
	printf("          ||*******                 ~~      欢迎使用     ~~                     *******||\n");
	printf("          ||*************************** please enter data *****************************||\n");
	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n");
	for (int i = 0; i < 10; i++) {
		if ((fp = fopen(name[i], "w+")) == NULL) {//打开文件
			printf("File open error!\n");
			exit(0);//退出程序,关闭流,防止异常
		}
		printf("          ||                   \\\\num    class    sex   linear_algebra_score//          ||\n");
		printf("                                                ******\n");
		scanf("                                  %s      %s       %s       %s", num[i], class3_4[i], sex[i], linear_algebra_score[i]);//键盘输入数据
		printf("          ||                   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////            ||\n");
		//fprintf(fp, "%s %s %s %s", num[i], class3_4[i], sex[i], linear_algebra_score[i]);//文件读入数据
		fputs(num[i], fp);
		fprintf(fp, " ");
		fputs(class3_4[i], fp);
		fprintf(fp, " ");
		fputs(sex[i], fp);
		fprintf(fp, " ");
		fputs(linear_algebra_score[i], fp);
		if (fclose(fp)) {
			printf("Can't close the file!\n");
			exit(0);
		}
	}
	system("pause");//暂停
	system("cls");//清屏
	for (int i = 0; i < 10; i++) {
		if ((fp = fopen(name[i], "r")) == NULL) {
			printf("File open error");
			exit(0);
		}
		while (!feof(fp)) {//看文件是否读到尾,防止出错
			fscanf(fp, "%s %s %s %s", student1[i].num1, student1[i].class3_41, student1[i].sex1, student1[i].linear_algebra_score1);
		}
		if (fclose(fp)) {
			printf("Can't close the file!\n");
			exit(0);
		}
	}
	system("pause");
	system("cls");
	system("color 0a");//颜色函数
	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n");
	printf("          ||**         ******       **                   **        ******            **||\n");
	printf("          ||*******                 ~~      感谢使用     ~~                     *******||\n");
	printf("          ||*************************** please output data*****************************||\n");
	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n");
	for (int i = 0; i < 10; i++) {
		strcpy(student1[i].name1, name2[i]);//将原有的的姓名数组复制到结构体的姓名数组
		printf("          ||                                                                           ||\n");
		printf("          ||           **          %s\t  %s\t%s\t%s\t%s          **         ||\n", student1[i].name1, student1[i].num1, student1[i].class3_41, student1[i].sex1, student1[i].linear_algebra_score1);
		//检验数据是否读入结构体
		printf("          ||                                                                           ||\n");
	}
	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n");
	//system("pause");
	Sleep(2500);//windows头文件里的函数,程序休眠2500秒
	return 0;
}


6. 总结

  1. 了解了文件的各种函数,并试验了一下
  2. 了解了system函数,system("pause")是暂停,system("cls")是清屏,system("color,0A")是改变字体颜色与背景颜色
  3. 尝试了改变界面
  4. 初步了解了windows函数,可惜没打算用
posted @ 2019-12-16 10:36  rghli(林洁颖)  阅读(230)  评论(0编辑  收藏  举报