• 博客园作业3


    完整代码
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    struct a {
    	char name[10];
    	char name1[10];
    	char classes[10];
    	char sex[10];
    	char grade[10];
    	char studentnumber[10];
    }a[100],str[100];
    int main()
    {
    	FILE* fp;
    	char name[10];
    	char name1[10];
    	char grade[10];
    	char classes[10];
    	char sex[10];
    	char studentnumber[10];
    	printf("请输入学生姓名:
    ");
    	for (int i = 0; i < 10; i++)
    	{
    		scanf("%s", a[i].name);
    		strcpy(a[i].name1, a[i].name);
    		strcat(a[i].name1, ".txt");
    		if ((fp = fopen(a[i].name1,"w+")) == NULL) {
    			printf("File open error!
    ");
    			exit(0);
    		}
    		else {
    			if (fclose(fp)) {
    				printf("Can not close the file!
    ");
    				exit(0);
    			}
    		}
    		
    	}
    	printf("请输入姓名,学号,性别,班级,线代成绩(中间以空格隔开)
    ");
    	for (int i = 0; i < 10; i++) {
    		if ((fp = fopen(a[i].name1, "r+")) == NULL) {
    			printf("File open error!
    ");
    			exit(0);
    		}
    			scanf("%s %s %s %s %s",a[i].name, a[i].studentnumber, a[i].sex, a[i].classes, a[i].grade);
    			fprintf(fp,"%s %s %s %s %s
    ",a[i].name, a[i].studentnumber, a[i].sex, a[i].classes, a[i].grade);
    			if (fclose(fp)) {
    				printf("Can not close the file!
    ");
    				exit(0);
    			}
    	}
    	printf("录入完成,✿✿ヽ(°▽°)ノ✿✿
    ");
    	
    	system("pause");
    	system("cls");
    
    	
    	for (int i = 0; i < 10; i++) {
    		if ((fp = fopen(str[i].name1, "r+")) == NULL) {
    			printf("File open error!
    ");
    			exit(0);
    		}
    		for (i = 0; i < 10; i++) {
    			fscanf(fp, "%s %s %s %s %s", str[i].name, str[i].studentnumber, str[i].sex, str[i].classes, str[i].grade);
    			printf("%s %s %s %s %s
    ", str[i].name, str[i].studentnumber, str[i].sex, str[i].classes, str[i].grade);
    		}
    		if (fclose(fp)) {
    			printf("Can not close the file!
    ");
    		}
    		printf("over!");
    		}
        return 0;
    }
    
    任务一:

    1.代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    struct a {
    	char name[10];
    	char name1[10];
    	char classes[10];
    	char sex[10];
    	char grade[10];
    	char studentnumber[10];
    }a[100],str[100];
    int main()
    {
    	FILE* fp;
    	char name[10];
    	char name1[10];
    	char grade[10];
    	char classes[10];
    	char sex[10];
    	char studentnumber[10];
    	printf("请输入学生姓名:
    ");
    	for (int i = 0; i < 10; i++)
    	{
    		scanf("%s", a[i].name);
    		strcpy(a[i].name1, a[i].name);//将学生姓名复制保留
    		strcat(a[i].name1, ".txt");//在姓名后加上txt后缀
    		if ((fp = fopen(a[i].name1,"w+")) == NULL) {       //创建文件
    			printf("File open error!
    ");
    			exit(0);
    		}
    		else {
    			if (fclose(fp)) {
    				printf("Can not close the file!
    ");
    				exit(0);
    			}
    		}
    	}
    

    2.运行截图

    任务二:
    printf("请输入姓名,学号,性别,班级,线代成绩(中间以空格隔开)
    ");
    	for (int i = 0; i < 10; i++) {
    		if ((fp = fopen(a[i].name1, "r+")) == NULL) {
    			printf("File open error!
    ");
    			exit(0);
    		}
    			scanf("%s %s %s %s %s",a[i].name, a[i].studentnumber, a[i].sex, a[i].classes, a[i].grade);
    			fprintf(fp,"%s %s %s %s %s
    ",a[i].name, a[i].studentnumber, a[i].sex, a[i].classes, a[i].grade);
    			if (fclose(fp)) {
    				printf("Can not close the file!
    ");
    				exit(0);
    			}
    	}
    	printf("录入完成,✿✿ヽ(°▽°)ノ✿✿
    ");
    

    任务三:
    for (int i = 0; i < 10; i++) {
    		if ((fp = fopen(str[i].name1, "r+")) == NULL) {//打开文件
    			printf("File open error!
    ");
    			exit(0);
    		}
    		for (i = 0; i < 10; i++) {
    			fscanf(fp, "%s %s %s %s %s", str[i].name, str[i].studentnumber, str[i].sex, str[i].classes, str[i].grade);
    			printf("%s %s %s %s %s
    ", str[i].name, str[i].studentnumber, str[i].sex, str[i].classes, str[i].grade);
    		}
    		if (fclose(fp)) {
    			printf("Can not close the file!
    ");
    		}
    		printf("over!");
    		}
        return 0;
    

    拓展:

    通过system函数调用DOS命令

    ASSOC 显示或修改文件扩展名关联。

    AT 计划在计算机上运行的命令和程序。

    ATTRIB 显示或更改文件属性。

    BREAK 设置或清除扩展式CTRL+C 检查。

    CACLS 显示或修改文件的访问控制列表(ACLs)。

    CALL 从另一个批处理程序调用这-一个。

    CD 显示当前目录的名称或将其更改。

    CHCP 显示或设置活动代码页数。

    CHDIR 显示当前目录的名称或将其更改。

    CIKDSK检 查磁盘并显示状态报告。

    CHKNTFS 1显示或修改启动时间磁盘检查。

    CIS清除屏幕。

    CMID 打开另一个Windows命令解释程序窗口。

    COMP 比较两个或两套文件的内容。

    COMPACT显示或更改 NIFS分区上文件的压缩。

    CONVERT将FAT 卷转换成NTFS。

    COPY 将至少一个文件复制到另一个位置。

    DATE 显示或设置日期。

    DEL 删除至少一个文件。

    DIR 显示一个目录中的文件和子目录

    DISKCOMP比较两个软盘的内容。

    DISKCOPY将一个软盘的内容复制到另-个软盘。

    DOSKEY编辑命令行、 调用Windows命令并创建宏。

    ECHO 显示消息,或将命令回显打开或关上。

    ENDLOCAL结束批文件中环境更改的本地化。

    ERASE 删除至少.个文件。

    EXIT 退出CMD.EXE程序(命令解释程序)。

    FC 比较两个或两套文件,并显示不同处。

    FIND 在文件中搜索文字字符串。FINDSTR在文件中搜索字符串。

    color 0A color后面的0是背景色代号,A是前景色代号
    (各颜色代码:0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色 )

  • 相关阅读:
    ORACLE 表移动后必须做的事
    浅谈oracle的监听服务静态注册
    SAP HANA2.0 EXPRESS 下载安装及管理
    ORA14452: attempt to create, alter or drop
    【JAVA】Macbook eclipse快速抽取函数快捷键(extract method)
    Linux 报错误:The remote server returned an error 500
    网页可能暂时无法连接,或者它已永久性地移动到了新网址。
    kafka 原理和总结
    Timer 原理 到 spring 定时器任务
    centos 安装kafka
  • 原文地址:https://www.cnblogs.com/lim-M/p/12077749.html
Copyright © 2020-2023  润新知