解读void student_information_load(struct information *stu) { //读入序号 fscanf(fp,"%d",&stu->number); //读入学号 fscanf(fp,"%s",stu->id); //读入姓名 fscanf(fp,"%s",stu->name); //读入性别 fscanf(fp,"%s",stu->gender); //读入班级 fscanf(fp,"%s",stu->clas); } void student_information_print(struct information *stu) { fputs(stu->name,fpp); fprintf(fpp,"\t"); fputs(stu->gender,fpp); fprintf(fpp,"\t"); fputs(stu->clas,fpp); fprintf(fpp,"\n"); }

时间: 2023-12-06 10:40:49 浏览: 30
这是两个函数,分别是读入学生信息和打印学生信息的函数。这里用到了结构体指针,结构体包含了学生的序号、学号、姓名、性别和班级等信息。 函数 `student_information_load()` 用于从文件中读取学生信息,输入参数为一个指向结构体的指针 `stu`。首先通过 `fscanf()` 函数读取学生的序号、学号、姓名、性别和班级等信息,然后将这些信息存储在结构体 `stu` 中。 函数 `student_information_print()` 用于将学生信息打印到文件中,输入参数也是一个指向结构体的指针 `stu`。函数中使用了 `fputs()` 和 `fprintf()` 函数将学生的姓名、性别和班级等信息写入到文件中。 这两个函数一般是作为学生信息管理系统的一部分,用于读取和打印学生信息。
相关问题

优化这段代码void student_information(struct stu **point) { //显示全部考生信息 FILE *fp; int i; struct stu *new; static struct stu *tail; if ((fp = fopen("C:/Users/MonnyX/Desktop/c/file2.dat", "r")) == NULL) { printf("打开文件失败!\n"); exit(0); } for (i = 1;i<=2; i++) { new = (struct stu *)malloc(sizeof(struct stu)); if (new == NULL) { printf("内存分配失败!\n"); exit(1); } fscanf(fp, "%d", &new->num); fscanf(fp, "%s", new->name); fscanf(fp, "%d", &new->age); fscanf(fp, "%s", new->sex); if (*point == NULL) { *point = new; new->next = NULL; } else { tail->next = new; new->next = NULL; } } tail = new; for(i=1;i<=2;i++){ printf("%d", new->num); printf("%s", new->name); printf("%d", new->age); printf("%s", new->sex); new=new->next; } fclose(fp); system("pause"); system("clear"); }

这段代码可以进行如下优化: 1. 将文件路径定义为常量或者宏,以便于修改和维护。 2. 将读取文件的代码封装成一个函数,可以复用。 3. 使用fgets()函数代替fscanf()函数,可以避免因为格式化字符串不匹配而导致的错误。 4. 将打印信息的代码封装成一个函数,可以复用。 5. 去掉不必要的变量和操作,简化代码结构。 6. 最好不要使用system()函数,因为它不可移植,可以使用跨平台的清屏函数。 下面是优化后的代码: ``` #define FILE_PATH "C:/Users/MonnyX/Desktop/c/file2.dat" void read_student_information(struct stu **point) { FILE *fp; int i; struct stu *new; static struct stu *tail; if ((fp = fopen(FILE_PATH, "r")) == NULL) { printf("打开文件失败!\n"); exit(0); } for (i = 1; i <= 2; i++) { new = (struct stu *)malloc(sizeof(struct stu)); if (new == NULL) { printf("内存分配失败!\n"); exit(1); } char buf[1024]; fgets(buf, sizeof(buf), fp); sscanf(buf, "%d %s %d %s", &new->num, new->name, &new->age, new->sex); if (*point == NULL) { *point = new; new->next = NULL; } else { tail->next = new; new->next = NULL; } tail = new; } fclose(fp); } void print_student_information(struct stu *point) { while (point != NULL) { printf("%d %s %d %s\n", point->num, point->name, point->age, point->sex); point = point->next; } } void student_information(struct stu **point) { read_student_information(point); print_student_information(*point); clear_screen(); } ```

int i; typedef struct student { int num; char name[20]; int score[3]; float avg; }student; void Inputdata(student* stu) { printf("请输入新学生的信息:\n"); printf("num name s1 s2 s3\n"); scanf("%d %s %d %d %d", &(stu->num), stu->name, &(stu->score[0]), &(stu->score[1]), &(stu->score[2])); stu->avg = (stu->score[0] + stu->score[1] + stu->score[2]) / 3.0; } void readdate(student stu[]) { FILE* fp = fopen("stu_ sort.txt", "r"); if (fp == NULL) { printf("read stu_ sort file error!"); return -1; } for (i = 0; i < 5; i++)//从文件中读入数据 { fscanf(fp, "%d %s %d %d %d %f", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].avg); } fclose(fp); } void Insertdata(student* oldstu, student* newstu,int n) { int pos = 0; while (pos < n) { if ((newstu->avg) < (oldstu[pos].avg)) break; pos++; } for (i = n; i > pos; i--) { memcpy(&oldstu[i],&oldstu[i-1], sizeof(student)); } memcpy(&oldstu[pos], newstu, sizeof(student)); } void writedate(student* stu) { FILE* fp = fopen("stu_new_sort.txt", "w"); if (fp == NULL) { printf("write file error!"); return -1; } for (i = 0; i < 6; i++)//使用fprint函数将结构体数组stu中的数据写入fp { fprintf(fp, "%d %s %d %d %d %f\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].avg); } fclose(fp); } int main() { student newstu;//首先定义一个新学生信息 Inputdata(&newstu);//输入这个新学生的信息 student oldstu[6]; readdate(oldstu);//调用函数从上一题排好序的文件中读入5个学生数据 Insertdata(oldstu, &newstu,5);//把新数据有序地插入到老数组中 writedate(oldstu);//把含有新数据的数组写入到新文件 return 0; }改写成正确的

int i; typedef struct student { int num; char name[20]; int score[3]; float avg; } student; void Inputdata(student* stu) { printf("请输入新学生的信息:\n"); printf("num name s1 s2 s3\n"); scanf("%d %s %d %d %d", &(stu->num), stu->name, &(stu->score[0]), &(stu->score[1]), &(stu->score[2])); stu->avg = (stu->score[0] + stu->score[1] + stu->score[2]) / 3.0; } int readdate(student stu[]) { FILE* fp = fopen("stu_sort.txt", "r"); if (fp == NULL) { printf("read stu_sort file error!"); return -1; } for (i = 0; i < 5; i++) { fscanf(fp, "%d %s %d %d %d %f", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].avg); } fclose(fp); return 0; } void Insertdata(student* oldstu, student* newstu, int n) { int pos = 0; while (pos < n) { if ((newstu->avg) < (oldstu[pos].avg)) break; pos++; } for (i = n; i > pos; i--) { memcpy(&oldstu[i], &oldstu[i - 1], sizeof(student)); } memcpy(&oldstu[pos], newstu, sizeof(student)); } int writedate(student* stu) { FILE* fp = fopen("stu_new_sort.txt", "w"); if (fp == NULL) { printf("write file error!"); return -1; } for (i = 0; i < 6; i++) { fprintf(fp, "%d %s %d %d %d %f\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].avg); } fclose(fp); return 0; } int main() { student newstu; Inputdata(&newstu); student oldstu[6]; if (readdate(oldstu) == -1) { return -1; } Insertdata(oldstu, &newstu, 5); if (writedate(oldstu) == -1) { return -1; } return 0; }

相关推荐

修改代码的错误 void xs(int n) { struct score stu[100]; int i; printf("成绩信息如下:\n"); FILE* fp; fp = fopen("C:\\2023-05\\student.txt", "r"); if (fp == NULL) { printf("打开文件失败,请重试!\n"); _getch(); fclose(fp); exit(0); } printf("学号\t姓名\tC语言\t高数\t大物\t总分\t名次\t平均分\n"); int c = 0; while (!feof(fp)) { fscanf(fp, "%s %s %d %d %d %d %d %f\n", &stu[c].snum, &stu[c].sname, &stu[c].chinese, &stu[c].math, &stu[c].english, &stu[c].sumstudent, &stu[c].rank, &stu[c].avg); c++; } if (n != 0) {//人数不为0 for (i = 0; i < c; i++) { printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\t%.1f\n", stu[i].snum, stu[i].sname, stu[i].chinese, stu[i].math, stu[i].english, stu[i].sumstudent, stu[i].rank, stu[i].avg); } } fclose(fp); printf("显示完毕 ,请按任意键返回\n"); _getch(); return; } void jisuan(int n) { struct score stu[100]; int i; printf("成绩信息如下:\n"); FILE* fp; fp = fopen("C:\\2023-05\\student.txt", "r"); if (fp == NULL) { printf("打开文件失败,请重试!\n"); _getch(); fclose(fp); exit(0); } int c = 0; while (!feof(fp)) { fscanf(fp, "%s %s %d %d %d %d %d %f\n", &stu[c].snum, &stu[c].sname, &stu[c].chinese, &stu[c].math, &stu[c].english, &stu[c].sumstudent, &stu[c].rank, &stu[c].avg); c++; } if (n != 0) {//人数不为0 float sum = 0; for (i = 0; i < c; i++) { sum = sum + stu[i].chinese; } printf("C语言平均分:%.1f\n", sum / c + 1); sum = 0; for (i = 0; i < c; i++) { sum = sum + stu[i].math; } printf("高数平均分:%.1f\n", sum / c + 1); sum = 0; for (i = 0; i < c; i++) { sum = sum + stu[i].english; } printf("大物平均分:%.1f\n", sum / c + 1); } fclose(fp); printf("显示完毕 ,请按任意键返回\n"); _getch(); return; }

/*有n个学生,每个学生的数据包括学号,C语言程序设计课程的平时成绩和期末考试成绩,测试数据已在主函数中,要求按平时成绩占30%,期末成绩占70%计算总评成绩, 并将总评成绩低于90分,且高于等于80分的同学的等级确定为"良好",并将这些同学的对应信息存放在结构体数组h中,人数通过函数返回值得到,输出相应的信息。 请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中注释语句之间填入所编写的若干语句。*/ #include <stdio.h> #include<string.h> #include<stdlib.h> #define N 100 typedef struct { char num[10];/*学号*/ int s1;/*期末成绩*/ int s2;/*平时成绩*/ float sum; /*总评*/ char level[10]; /*等级*/ } STU; int fun (STU a[],int n,STU h[]) { /************Begin************/ /************End**************/ } int main() { STU s[N]={{"GA05",85,76},{"GA03",76,90},{"GA02",69,90},{"GA04",85,56},{"GA01",91,95}, {"GA07",72,80},{"GA08",64,45}, {"GA06",87,98},{"GA015",85,86},{"GA013",91,97}},h[N]; int i,k,n=10; FILE *out,*in; k=fun(s,n,h); printf("There are :\n"); for(i=0;i<k;i++) printf("%s %d %d %.2f %s\n",h[i].num,h[i].s1,h[i].s2,h[i].sum,h[i].level); /******************************/ in=fopen("in68.dat","r"); out=fopen("out68.dat","w"); i=0; while(!feof(in)) { fscanf(in,"%s %d %d",s[i].num,&s[i].s1,&s[i].s2); i++; } n=i; k=fun(s,n,h); for(i=0;i<k;i++) fprintf(out,"%s %d %d %.2f %s\n",h[i].num,h[i].s1,h[i].s2,h[i].sum,h[i].level); fclose(out); fclose(in); /******************************/ system("pause"); return 0; }

最新推荐

recommend-type

源代码-ajax即时聊天程序(新手学习推荐).zip

源代码-ajax即时聊天程序(新手学习推荐).zip
recommend-type

python读取excel数据.doc

以下是一个简单的Python代码,用于在控制台上打印一个看起来像爱心的形状。这个代码使用了ASCII字符来创建形状。 python print('\n'.join([''.join([('Love'[(x-y)%4] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ' ') for x in range(-30, 30)]) for y in range(15, -15, -1)])) 这个代码使用了数学公式来生成心形。但是,由于ASCII字符的限制,这个心形可能看起来不是很完美。 如果你想要一个更详细和定制化的心形,你可能需要使用图形库,如PIL(Python Imaging Library)或matplotlib。但是,这些库通常用于创建图像文件或在图形用户界面上绘制,而不是在控制台上打印。 另外,这里有一个使用turtle模块在图形窗口中绘制爱心的简单示例: python import turtle # 创建一个新的turtle对象 heart = turtle.Turtl
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

云原生架构与soa架构区别?

云原生架构和SOA架构是两种不同的架构模式,主要有以下区别: 1. 设计理念不同: 云原生架构的设计理念是“设计为云”,注重应用程序的可移植性、可伸缩性、弹性和高可用性等特点。而SOA架构的设计理念是“面向服务”,注重实现业务逻辑的解耦和复用,提高系统的灵活性和可维护性。 2. 技术实现不同: 云原生架构的实现技术包括Docker、Kubernetes、Service Mesh等,注重容器化、自动化、微服务等技术。而SOA架构的实现技术包括Web Services、消息队列等,注重服务化、异步通信等技术。 3. 应用场景不同: 云原生架构适用于云计算环境下的应用场景,如容器化部署、微服务
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

数字舵机控制程序流程图

以下是数字舵机控制程序的流程图: ![数字舵机控制程序流程图](https://i.imgur.com/2fgKUQs.png) 1. 初始化引脚:设置舵机控制引脚为输出模式。 2. 初始化舵机:将舵机控制引脚输出的PWM信号设置为初始值,初始化舵机的位置。 3. 接收控制信号:通过串口或者其他方式接收舵机控制信号。 4. 解析控制信号:解析接收到的控制信号,确定舵机需要转动的角度和方向。 5. 转动舵机:根据解析后的控制信号,设置舵机控制引脚输出的PWM信号的占空比,使舵机转动到目标位置。 6. 延时:为了保证舵机转动到目标位置后稳定,需要延时一段时间。 7. 返回接收控制信