1:使用fscanf 和 fprintf 实现文件的拷贝功能
先编写以下结构体
struct studentf
char name[20];
double math;
double chinese;
double english;
double physical;
double chemical;
double biological;
2.
第一步:创建一个 struct student 类型的数组 arr[3],初始化该数组中3个学生的属性第二步:编写一个叫做save的函数,功能为
将数组arr中的3个学生的所有信息,保存到文件中去,使用fprintf实现第三步:编写一个叫做load的函数,功能为
将文件中保存的3个学生信息,读取后,写入到另一个数组brr中去第四步:编写一个叫做 show的函数,功能为
遍历输出 arr 或者 brr 数组中的所有学生的信息第五步:编写一个叫做 setMath 的函数,功能为
修改 文件中 所有学生的数学成绩
要求:用链表写
头文件
#ifndef __STUDENT_H__
#define __STUDENT_H__
#include<stdio.h>
#include<stdlib.h>
#define SIZE 3
typedef struct data
{
char name[20];
double math;
double chinese;
double english;
double physical;
double chemical;
double biological;
}data;
typedef struct student
{
union
{
int len;
data stu;
};
struct student* next;
}node;
void create_link(node** p);
node* create_node(data* arr);
void add_link(node* p,data* arr);
void show_link(node* p);
void save(node* p,const char* filename);
void load(data* arr,const char* filename);
void show(data* arr);
void setMath(node* p,const char* filename);
void free_link(node** p);
#endif
源文件
#include "student.h"
void create_link(node** p)
{
*p = (node*)malloc(sizeof(node));
if(NULL == *p)
{
printf("创建失败");
return;
}
(*p)->len = 0;
(*p)->next =NULL;
}
node* create_node(data* arr)
{
node* p = (node*)malloc(sizeof(node));
if(NULL == p)
{
return NULL;
}
p->next = NULL;
p->stu = *arr;
}
void add_link(node* p,data* arr)
{
if(NULL == p)
{
return;
}
node* ptr = create_node(arr);
node* ptr1 = p;
while(ptr1->next != NULL)
{
ptr1 = ptr1->next;
}
ptr1->next = ptr;
p->len++;
}
void show_link(node* p)
{
if(NULL == p)
{
return;
}
node* ptr = p->next;
while(ptr != NULL)
{
printf("%s %lf %lf %lf %lf %lf %lf\n",(ptr->stu).name,(ptr->stu).math,(ptr->stu).chinese,(ptr->stu).english,(ptr->stu).physical,(ptr->stu).chemical,(ptr->stu).biological);
ptr = ptr->next;
}
}
/*********************************************/
void save(node* p,const char* filename)
{
FILE* fp = fopen(filename,"w");
node* ptr = p->next;
while(ptr != NULL)
{
fprintf(fp,"%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",ptr->stu.name,ptr->stu.math,ptr->stu.chinese,ptr->stu.english,ptr->stu.physical,ptr->stu.chemical,ptr->stu.biological);
ptr = ptr->next;
}
fclose(fp);
}
void load(data* arr,const char* filename)
{
FILE* fp = fopen(filename,"r");
int i = 0;
while(1)
{
int flag = fscanf(fp,"%s%lf%lf%lf%lf%lf%lf",arr[i].name,&arr[i].math,&arr[i].chinese,&arr[i].english,&arr[i].physical,&arr[i].chemical,&arr[i].biological);
if(flag == EOF)
break;
i++;
}
fclose(fp);
}
void show(data* arr)
{
for(int i=0; i<SIZE; i++)
{
printf("%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",arr[i].name,arr[i].math,arr[i].chinese,arr[i].english,arr[i].physical,arr[i].chemical,arr[i].biological);
}
}
void setMath(node* p,const char* filename)
{
FILE* fp = fopen(filename,"w");
node* ptr = p->next;
while(ptr != NULL)
{
scanf("%lf",&ptr->stu.math);
ptr = ptr->next;
}
save(p,"student.txt");
}
void free_link(node** p)
{
if(NULL == *p)
{
return;
}
while((*p)->next != NULL)
{
node* ptr = (*p)->next;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
free(ptr);
ptr = NULL;
}
free(*p);
*p = NULL;
}
测试文件
#include "student.h"
int main()
{
data arr[SIZE] = {{"asd",1,2,3,4,5,6},{"qwe",1,2,3,4,5,6},{"zxc",1,2,3,4,5,6}};
data brr[SIZE] = {0};
node* p = NULL;
create_link(&p);
for(int i=0;i<SIZE;i++)
{
add_link(p,arr+i);
}
show_link(p);
save(p,"student.txt");
load(brr,"student.txt");
show(brr);
setMath(p,"student.txt");
}
结果