zoukankan      html  css  js  c++  java
  • 文件初学

    文件初学

    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!
    ");
    			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
    ");
    	system("color 1a");
    	for (int i = 0; i < 10; i++) {
    		if ((fp = fopen(name[i], "w+")) == NULL) {//打开文件
    			printf("File open error!
    ");
    			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!
    ");
    			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
    ");
    	system("color 1a");
    	for (int i = 0; i < 10; i++) {
    		if ((fp = fopen(name[i], "w+")) == NULL) {//打开文件
    			printf("File open error!
    ");
    			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!
    ");
    			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!
    ");
    			exit(0);
    		}
    	}
    	system("pause");
    	system("cls");
    	system("color 0a");//颜色函数
    	printf("you will find name num class sex linear_algebra_score
    ");
    	for (int i = 0; i < 10; i++) {
    		strcpy(student1[i].name1, name2[i]);//将原有的的姓名数组复制到结构体的姓名数组
    		printf("%s	%s	%s	%s	%s
    ",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!
    ");
    			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
    ");
    	system("color 0a");
    	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    ");
    	printf("          ||**         ******       **                   **        ******            **||
    ");
    	printf("          ||*******                 ~~      欢迎使用     ~~                     *******||
    ");
    	printf("          ||*************************** please enter data *****************************||
    ");
    	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    ");
    	for (int i = 0; i < 10; i++) {
    		if ((fp = fopen(name[i], "w+")) == NULL) {//打开文件
    			printf("File open error!
    ");
    			exit(0);//退出程序,关闭流,防止异常
    		}
    		printf("          ||                   \\num    class    sex   linear_algebra_score//          ||
    ");
    		printf("                                                ******
    ");
    		scanf("                                  %s      %s       %s       %s", num[i], class3_4[i], sex[i], linear_algebra_score[i]);//键盘输入数据
    		printf("          ||                   \\\\\\\\\\\\\\\\\\\\////////////////////////            ||
    ");
    		//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!
    ");
    			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!
    ");
    			exit(0);
    		}
    	}
    	system("pause");
    	system("cls");
    	system("color 0a");//颜色函数
    	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    ");
    	printf("          ||**         ******       **                   **        ******            **||
    ");
    	printf("          ||*******                 ~~      感谢使用     ~~                     *******||
    ");
    	printf("          ||*************************** please output data*****************************||
    ");
    	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    ");
    	for (int i = 0; i < 10; i++) {
    		strcpy(student1[i].name1, name2[i]);//将原有的的姓名数组复制到结构体的姓名数组
    		printf("          ||                                                                           ||
    ");
    		printf("          ||           **          %s	  %s	%s	%s	%s          **         ||
    ", student1[i].name1, student1[i].num1, student1[i].class3_41, student1[i].sex1, student1[i].linear_algebra_score1);
    		//检验数据是否读入结构体
    		printf("          ||                                                                           ||
    ");
    	}
    	printf("          |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    ");
    	//system("pause");
    	Sleep(2500);//windows头文件里的函数,程序休眠2500秒
    	return 0;
    }
    
    
    

    6. 总结

    1. 了解了文件的各种函数,并试验了一下
    2. 了解了system函数,system("pause")是暂停,system("cls")是清屏,system("color,0A")是改变字体颜色与背景颜色
    3. 尝试了改变界面
    4. 初步了解了windows函数,可惜没打算用
  • 相关阅读:
    《完结篇》论文笔记
    《QD超市管理系统的设计与实现》论文笔记(十九)
    《基于JSP的超市管理系统设计与实现》论文笔记(十八)
    《B/S模式下ADO.NET数据库访问技术的设计及应用》论文笔记(十七)
    《基于B/S中小型超市进销存管理系统的研究与设计》论文笔记(十六)
    《基于Web的Git可视化设计与实现》论文笔记(十五)
    《小型超市商品管理系统的设计与实现》论文笔记(十四)
    《超市商品管理系统的设计与实现》论文笔记(十三)
    《中小连锁超市管理系统的设计与实现》论文笔记(十二)
    laravels安装horizon和Supervisor
  • 原文地址:https://www.cnblogs.com/tylk/p/12047852.html
Copyright © 2011-2022 走看看