/*
把结构内容保存到文件中。
由于结构可以储存不同类型的信息,所以它是构建数据库的重要工具。
例如,可以用一个结构储存雇员或汽车零件的相关信息。最终,我们要把这些信息储存在文件中,并且能再次检索。
数据库文件可以包含任意数量的此类数据对象。
储存在一个结构中的整套信息被称为记录(record),单独的项被称为字段(field)。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 2
char * s_gets(char * st, int n);
struct book
{
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS];
int count = 0;
int index, filecount;
FILE *pbooks;
int size=sizeof(struct book);
if ((pbooks = fopen("book.dat","a+b")) ==NULL)
{
fputs("Can't open book.dat file\n",stderr);
exit(1);
}
rewind(pbooks); /* 定位到文件开始 */
/*
从文件里读取所有结构体信息
*/
while (count < MAXBKS && fread(&library[count],size,1,pbooks) == 1)
{
if(0 == count)
{
puts("Current con
C基础-结构体-8-把结构内容保存到文件中
最新推荐文章于 2022-10-15 18:39:28 发布