1.思维导图
2.使用fread和fwrite函数,重写昨天的第二个作业
1>程序代码
w1.h
#ifndef W1_H
#define W1_H#include <stdio.h>
#include <stdlib.h>
#include <string.h>// 定义学生结构体
typedef struct student {char name[20];double math;double chinese;double english;double physical;double chemical;double biological;struct student *next; // 指向下一个节点
} Stu, *StuPtr;// 函数声明
void save(StuPtr head);
StuPtr load(const char *filename);
void show(StuPtr head);
void setMath(const char *filename, double math);#endif
w1.c
#include "w1.h"// 保存链表中的学生信息到文件
void save(StuPtr head) {FILE *fp = fopen("demo1.bin", "w");if (fp == NULL) {printf("打开失败\n");return;}StuPtr current = head;while (current) {fwrite(current,sizeof(Stu),1,fp);current = current->next;}fclose(fp);printf("学生信息已保存到文件\n");
}// 从文件中读取学生信息到链表
StuPtr load(const char *filename) {FILE *fp = fopen(filename, "r");if (fp == NULL) {printf("文件打开失败\n");return NULL;}StuPtr head = NULL, tail = NULL;while (1) {StuPtr newStudent = (StuPtr)malloc(sizeof(Stu));if((fread(newStudent,sizeof(Stu),1,fp)!=1)){free(newStudent);break;}newStudent->next = NULL;if (head == NULL) {head = newStudent;tail = newStudent;} else {tail->next = newStudent;tail = newStudent;}}fclose(fp);printf("学生信息已从文件加载到链表\n");return head;
}// 显示链表中的学生信息
void show(StuPtr head) {StuPtr current = head;printf("姓名\t数学\t语文\t英语\t物理\t化学\t生物\n");while (current) {printf("%s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", current->name,current->math,current->chinese,current->english,current->physical,current->chemical,current->biological);printf("----------------------------------------------------------\n");current = current->next;}
}// 修改链表中所有学生的数学成绩并保存到文件
void setMath(const char *filename, double math) {StuPtr head = load(filename);StuPtr current = head;while (current) {current->math = math;current = current->next;}save(head);// 释放链表current = head;while (current) {StuPtr temp = current;current = current->next;free(temp);}printf("所有学生的数学成绩已修改为 %lf\n", math);
}
main.c
#include "w1.h"int main() {// 创建链表头结点StuPtr head = NULL, tail = NULL;// 初始化链表中的学生信息StuPtr student1 = (StuPtr)malloc(sizeof(Stu));strcpy(student1->name, "张三");student1->math = 78;student1->chinese = 88;student1->english = 98;student1->physical = 66;student1->chemical = 87;student1->biological = 93;student1->next = NULL;StuPtr student2 = (StuPtr)malloc(sizeof(Stu));strcpy(student2->name, "李四");student2->math = 55;student2->chinese = 66;student2->english = 55;student2->physical = 55;student2->chemical = 55;student2->biological = 55;student2->next = NULL;StuPtr student3 = (StuPtr)malloc(sizeof(Stu));strcpy(student3->name, "王五");student3->math = 66;student3->chinese = 66;student3->english = 6;student3->physical = 66;student3->chemical = 66;student3->biological = 66;student3->next = NULL;// 将学生加入链表head = student1;tail = student1;tail->next = student2;tail = student2;tail->next = student3;// 保存链表到文件save(head);// 加载学生信息到新链表StuPtr newHead = load("demo1.bin");// 显示链表中的学生信息printf("初始学生信息:\n");show(newHead);// 修改数学成绩并保存setMath("demo1.bin", 100);// 加载修改后的学生信息并显示StuPtr updatedHead = load("demo1.bin");printf("修改后的学生信息:\n");show(updatedHead);// 释放链表StuPtr current = updatedHead;while (current) {StuPtr temp = current;current = current->next;free(temp);}return 0;
}
2>运行结果显示
3.使用fread和fwrite将一张任意bmp图片改成德旗
1>程序代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{FILE *fp = fopen("rising_freedom.bmp","r+");if(fp == NULL){perror("fopen");return 1;}int bmp_size = 0,bmp_width = 0,bmp_height = 0;fseek(fp,2,SEEK_SET);fread(&bmp_size,4,1,fp);printf("图片的大小为:%d\n",bmp_size);fseek(fp,18,SEEK_SET);fread(&bmp_width,4,1,fp);fread(&bmp_height,4,1,fp);printf("图片的像素格式为:%d * %d\n",bmp_width,bmp_height);fseek(fp,54,SEEK_SET);unsigned char bgr1[3] = {0,0,0};//黑色unsigned char bgr2[3] = {0,0,255};//红色unsigned char bgr3[3] = {0,255,255};//黄色for(int i=0;i<bmp_width * bmp_height/3;i++){fwrite(bgr3,3,1,fp); }for(int i=0;i<bmp_width *bmp_height/3;i++){fwrite(bgr2,3,1,fp);}for(int i=0;i<bmp_width *bmp_height/3;i++){fwrite(bgr1,3,1,fp);}fclose(fp);return 0;
}