#include
#include
#define bookchang 10/*书籍信息字符串长度*/
#include
static struct book /*书籍的信息结构体*/
{char code[bookchang];/* 图书出版号 /
char name[bookchang];/ 书名 /
char author[bookchang];/ 作者 /
char chubanshe[bookchang];/ 出版社 /
int year;
int month;
int day;
int rent;/ 书籍的借出数量 /
int have;/ 书籍的可借量 /
int count;/ 书籍的总数量 /
char passcode;/ 书籍的序列号 /
char control;/ 书籍录入的控制指令 /
struct book*to;/ 去向指针 /
struct book*foe;/ 回向指针 */
};
static char wuyong[2]="";
struct book*bookreadin(FILE*fp)
{
struct book*booktemp;
booktemp=(struct book*)calloc(1,sizeof(struct book));
fread(booktemp,sizeof(book),1,fp);
return booktemp;
}
void bookwritein(struct book*booktemp,FILE*fp)
{
fwrite(booktemp,sizeof(book),1,fp);
return;
}
void main()
{FILE*fp;
if((fp=fopen("book.txt","ab+"))==NULL)
{printf("open failed!!!\n");
exit(0);
}
struct book*a;
strcpy(a->name,"111");
bookwritein(a,fp);
fclose(fp);
if((fp=fopen("book.txt","ab+"))==NULL)
{printf("open failed!!!\n");
exit(0);
}
a=bookreadin(fp);
fclose(fp);
printf("%s",a->name);
}
至少这里是错的:
struct book*a;
strcpy(a->name,"111");
指针a还没有初始化被分配内存,不可以写内容进去
struct book*a;
strcpy(a->name,"111");