C语言:多种方式将结构体存放到文件中

第一种方法:使用格式化读写文本文件

#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;
        

        //将文件数据输出

}

结果:

8759e4471c0413ad61c9057cb81fb13b1b4.jpg

转载于:https://my.oschina.net/1024and1314/blog/3100328

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值