求把结构体里的数据读写入文件

//预处理 

#include<stdio.h>  

#include<string.h>

#include<stdlib.h>

#include<conio.h>

 

//定义结构体 

struct book

{

 int id;

 char name[30];

 char author[20];

 char publish[20];

 int store;

 int total;

 int user[10];

 int days[90];

}books[100];

struct book

{
 int id;

 char name[30];

 char author[20];

 char publish[20];

 int store;

 int total;

 char user[10];

 char days[90];

}books[100];

void savebooks()
{
    FILE *fp;
    fp = fopen("books.txt","r");
    if(fp == NULL)
        return;
    for(int i=0;i<100;i++)
    {
        fprintf(fp,"%d\t%s\t%s\t%s\t%d\t%d\t%s\t%s\n",books[i].num,books[i].name,books[i].author,books[i].publish,books[i].store,books[i].total,books[i].user,books[i].days);

    }
    fclose(fp);
}

 

最后两个成员user和days应该也是char 数组吧?为什么是int数组呢?如果确实是,那应该单独用for循环逐个写入,可以用TAB键或者空格分隔

用File对象保存就可以了,参考:

void lr()
{ 
	FILE *p; 
	struct st s1; 
	char ans='y'; 
	p=fopen("d:\\staff.dat","a"); 
	if(p==NULL) { 
		printf("file open error!\n");
 		exit(-1); 
	} 
	while(ans=='y'||ans=='Y') { 
		printf("请输入工号:"); 
		scanf("%s",s1.num); 
		printf("请输入姓名:"); 
		scanf("%s",s1.name); 
		printf("请输入年龄:"); 
		scanf("%d",&s1.age); 
		printf("请输入工作:"); 
		scanf("%s",s1.work); printf("请输入性别:"); 
		scanf("%s",s1.sex); 
		printf("请输入地址:"); 
		scanf("%s",s1.add); 
		printf("请输入电话:"); 
		scanf("%s",s1.phone); 
		printf("请输入入职时间(年月日):"); 
		scanf("%s%s%s",s1.hiredate.year,s1.hiredate.month,s1.hiredate.day); 
		fwrite(&s1,sizeof(struct st),1,p); 
		getchar(); 
		printf("继续输入吗?(y/n)"); 
		scanf("%c",&ans); 
	} 
	fclose(p);
}