第一种方法:使用格式化读写文本文件
#include<stdio.h>
#include<stdlib.h>
//主要功能:将特定结构写入文件,和文件读取
#define SIZE 2
//定义员工结构
struct Student
{
char name[30]; //姓名
char department[30]; //部门
float base_pay; //基本工资
float allowance; //月工资
float total;
}*pa,*pb;
int main(){
//结构体数组
int i=0;
//文件
struct Student stu[SIZE];
FILE *fp;
pa = stu;
pb = stu;
fp = fopen("C:\\Users\\32239\\Desktop\\student.txt","wt+");
if(fp==NULL){
printf("error!\n");
exit(0);
}
printf("please input name/department/base_pay/allowance:\n");
for(i= 0; i< SIZE; i++,pa++){
scanf("%s %s %f %f",pa->name,pa->department,&pa->base_pay,&pa->allowance);
//将数据读到文件中
pa->total = pa->base_pay + pa->allowance;
fprintf(fp,"%s %s %f %f %f\n",pa->name,pa->department,pa->base_pay,pa->allowance,pa->total);
}
rewind(fp);
for(i= 0; i< SIZE; i++,pb++){
fscanf(fp,"%s %s %f %f %f\n",pb->name,pb->department,&pb->base_pay,&pb->allowance,&pb->total);
pb->base_pay += 100;
pb->total = pb->base_pay + pb->allowance;
printf("%s,%s ,%f ,%f ,%f\n",pb->name,pb->department,pb->base_pay,pb->allowance,pb->total);
}
fclose(fp);
return 0;
//将文件数据输出
}
#include<stdio.h>
#include<stdlib.h>
//主要功能:将特定结构写入文件,和文件读取
#define SIZE 2
//定义员工结构
struct Student
{
char name[30]; //姓名
char department[30]; //部门
float base_pay; //基本工资
float allowance; //月工资
float total;
};
int main(){
//结构体数组
int i=0;
//文件
struct Student stu[SIZE];
FILE *fp;
fp = fopen("C:\\Users\\32239\\Desktop\\student.txt","wt+");
if(fp==NULL){
printf("error!\n");
exit(0);
}
printf("please input name/department/base_pay/allowance:\n");
for(i= 0; i< SIZE; i++){
scanf("%s %s %f %f",stu[i].name,stu[i].department,&stu[i].base_pay,&stu[i].allowance);
//将数据读到文件中
stu[i].total = stu[i].base_pay + stu[i].allowance;
fputs(&stu[i],fp);
//fprintf(fp,"%s %s %f %f %f\n",stu[i].name,stu[i].department,stu[i].base_pay,stu[i].allowance,stu[i].total);
}
rewind(fp);
for(i= 0; i< SIZE; i++){
fgets(&stu[i],sizeof(Student),fp);
//fscanf(fp,"%s %s %f %f %f\n",stu[i].name,stu[i].department,&stu[i].base_pay,&stu[i].allowance,&stu[i].total);
stu[i].base_pay += 100;
stu[i].total =stu[i].base_pay + stu[i].allowance;
printf("%s,%s ,%f ,%f ,%f\n",stu[i].name,stu[i].department,stu[i].base_pay,stu[i].allowance,stu[i].total);
}
fclose(fp);
return 0;
//将文件数据输出
}
总结:最好使用格式化读写结构体数据,fgets不行,不能保证里面没有控制符
第二种:使用数据块读写二进制文件,不用编码,直接以二进制存取,不过打开不可读
#include<stdio.h>
#include<stdlib.h>
//主要功能:将特定结构写入文件,和文件读取
#define SIZE 2
//定义员工结构
#define LENGHT sizeof(struct Student)
struct Student
{
char name[30]; //姓名
char department[30]; //部门
float base_pay; //基本工资
float allowance; //月工资
float total;
};
int main(){
//结构体数组
int i=0;
//文件
struct Student stu[SIZE];
FILE *fp;
fp = fopen("C:\\Users\\32239\\Desktop\\student.txt","wb+");
if(fp==NULL){
printf("error!\n");
exit(0);
}
printf("please input name/department/base_pay/allowance:\n");
for(i= 0; i< SIZE; i++){
scanf("%s %s %f %f",stu[i].name,stu[i].department,&stu[i].base_pay,&stu[i].allowance);
//将数据读到文件中
stu[i].total = stu[i].base_pay + stu[i].allowance;
if((fwrite(&stu[i], LENGHT,1,fp)) != 1){
exit(0);
}
//fprintf(fp,"%s %s %f %f %f\n",stu[i].name,stu[i].department,stu[i].base_pay,stu[i].allowance,stu[i].total);
}
rewind(fp);
for(i= 0; i< SIZE; i++){
fread(&stu[i],LENGHT,1,fp);
//fscanf(fp,"%s %s %f %f %f\n",stu[i].name,stu[i].department,&stu[i].base_pay,&stu[i].allowance,&stu[i].total);
stu[i].base_pay += 100;
stu[i].total =stu[i].base_pay + stu[i].allowance;
printf("%s,%s ,%f ,%f ,%f\n",stu[i].name,stu[i].department,stu[i].base_pay,stu[i].allowance,stu[i].total);
}
fclose(fp);
return 0;
//将文件数据输出
}
结果: