当前位置: 首页 > news >正文

作业:IO:day2

题目一

第一步:创建一个 struct Student 类型的数组 arr[3],初始化该数组中3个学生的属性

第二步:编写一个叫做save的函数,功能为 将数组arr中的3个学生的所有信息,保存到文件中去,使用fread实现fwrite

第三步:编写一个叫做load的函数,功能为 将文件中保存的3个学生信息,读取后,写入到另一个数组 brr 中去

第四步:编写一个叫做 show的函数,功能为 遍历输出 arr 或者 brr 数组中的所有学生的信息

第五步:编写一个叫做 setMath 的函数,功能为 修改 文件中 所有学生的数学成绩

代码

#include "student.h"NodePtr createHead()
{NodePtr H=(NodePtr)malloc(sizeof(Node));if(NULL==H){printf("申请空间失败\n");return NULL;}H->tail=H;H->next=NULL;printf("头结点初始化完毕\n\n");return H;
}
NodePtr addData(NodePtr H)
{NodePtr newnode=(NodePtr)malloc(sizeof(Node));if(NULL==newnode){printf("新节点申请失败\n");return NULL;}//原最后一个数据 指向处理H->tail->next=newnode;//头结点指向处理if(NULL==H->next){H->next=newnode;//头指针处理}H->tail=newnode;//尾指针处理//本节点指针域处理newnode->next=NULL;//本节点数据域处理printf("姓名:");fscanf(stdin,"%s",newnode->data.name);printf("数学:");fscanf(stdin,"%lf",&newnode->data.math);printf("语文:");fscanf(stdin,"%lf",&newnode->data.chinese);printf("英语:");fscanf(stdin,"%lf",&newnode->data.english);printf("物理:");fscanf(stdin,"%lf",&newnode->data.physical);printf("化学:");fscanf(stdin,"%lf",&newnode->data.chemical);printf("生物:");fscanf(stdin,"%lf",&newnode->data.biological);return newnode;
}void save(NodePtr H,const char* Name_of_File)
{if(NULL==H){printf("保存失败:地址不合法\n");return;}//打开文件FILE* wfp=fopen(Name_of_File,"w");NodePtr temp=H;//用于遍历while(temp->next!=NULL){temp=temp->next;fwrite(&temp->data,sizeof(temp->data),1,wfp);}//关闭文件fclose(wfp);printf("数据已写入:%s\n\n",Name_of_File);
}void show(NodePtr H)
{NodePtr temp=H;if(NULL==H){printf("地址不合法\n");return;}while(temp->next!=NULL){temp=temp->next;printf("%s ",temp->data.name);printf("%lf ",temp->data.math);printf("%lf ",temp->data.chinese);printf("%lf ",temp->data.english);printf("%lf ",temp->data.physical);printf("%lf ",temp->data.chemical);printf("%lf\n",temp->data.biological);}printf("链表显示完毕\n\n");}void load(NodePtr H,const char* Name_of_File)
{//打开文件FILE* rfp=fopen(Name_of_File,"r");while(1){NodePtr	newnode=(NodePtr)malloc(sizeof(Node));int res=fread(&newnode->data,sizeof(newnode->data),1,rfp);if(EOF==res|| 0==res){free(newnode);newnode=NULL;break;}//指向处理//原最后一个数据 指向处理H->tail->next=newnode;H->tail=newnode;//尾指针处理//本节点指针域处理newnode->next=NULL;//本节点数据域处理}//关闭文件fclose(rfp);printf("数据已载入链表\n\n");
}

结果

题目

使用 fread 和 fwrite 将一张任意bmp图片改成德旗

代码

int main(int argc, const char *argv[])
{FILE* wfp=fopen("photo.bmp","r+");int width=0,height=0;fseek(wfp,18,SEEK_SET);fread(&width,4,1,wfp);fread(&height,4,1,wfp);fseek(wfp,54,SEEK_SET);unsigned char red[3]={0,0,255};unsigned char yellow[3]={0,255,255};//黄色unsigned char black[3]={0,0,0};//黑色for(int i=0;i<width*height;i++){if(i>width*height*2/3){fwrite(black,3,1,wfp);}else if(i>width*height/3){fwrite(red,3,1,wfp);}else{fwrite(yellow,3,1,wfp);}}fclose(wfp);return 0;
}

结果

http://www.lryc.cn/news/517767.html

相关文章:

  • UVM: TLM机制
  • flink的EventTime和Watermark
  • arcgis的合并、相交、融合、裁剪、联合、标识操作的区别和使用
  • 【Leetcode 热题 100】20. 有效的括号
  • 比较procfs 、 sysctl和Netlink
  • Leetcode 3413. Maximum Coins From K Consecutive Bags
  • MakeFile使用指南
  • 矩阵碰一碰发视频的视频剪辑功能源码搭建,支持OEM
  • VB.NET CRC32 校验
  • 冒充者综合征上线了
  • 【大模型】百度千帆大模型对接LangChain使用详解
  • Redis相关面试
  • 使用强化学习训练神经网络玩俄罗斯方块
  • java中的日期处理:只显示日期,不显示时间的两种处理方式
  • 腾讯云AI代码助手编程挑战赛——贪吃蛇小游戏
  • 水水水水水
  • Spring整合SpringMVC
  • 【Rust自学】10.4. trait Pt.2:trait作为参数和返回类型、trait bound
  • 嵌入式系统 (2.嵌入式硬件系统基础)
  • Linux 下 Vim 环境安装踩坑问题汇总及解决方法(重置版)
  • OpenAI 故障复盘 - 阿里云容器服务与可观测产品如何保障大规模 K8s 集群稳定性
  • 安卓触摸对焦
  • jupyter出现“.ipynb appears to have died. It will restart automatically.”解决方法
  • 20250108-实验+神经网络
  • 【权限管理】CAS(Central Authentication Service)
  • Golang笔记:使用net包进行TCP监听回环测试
  • 《浮岛风云》V1.0中文学习版
  • Day10——爬虫
  • 10. C语言 函数详解
  • NRC优先级中比较特殊的—NRC0x13和NRC0x31