1. Structure write to file, read
#include <stdio.h>#include<string.h>#include<stdlib.h>#defineMax 5structbooks{Chartitle[ -]; Charauthor[ -]; intPrice ;};Const Char* Filename="Books.txt";intSize=sizeof(structbooks);voidReadstructBooks bks[]);voidWritestructBooks bks[]);voidListstructBooks bks[]);voidDemostructBooks bks[]);voidMainvoid){ structBooks Bks[max]; //write (BKS);read (BKS);}voidWritestructBooks bks[]) {FILE*FP; intI=0; intaddlist; if(Fp=fopen (FileName,"a+b")) ==NULL) {printf ("File Open fail"); } puts ("hero, you want to add a few data?"); scanf ("%d",&addlist); if(addlist>0) { Do{printf ("Add new book title\n"); scanf ("%s",&bks[i].title); printf ("Add new book author\n"); scanf ("%s",&Bks[i].author); printf ("Add new book price\n"); scanf ("%d",&Bks[i].price); I++; Fwrite (&bks[i],size,1, FP); } while(i<addlist); } }voidReadstructBooks bks[]) {FILE*FP; intI=0; if(Fp=fopen (FileName,"r+")) ==NULL) {printf ("File Open fail"); } rewind (FP); while(I<max && fread (&bks[i],size,1, fp) = =1) {printf ("title is%s,author are%s,price is%d\n", Bks[i].title,bks[i].author,bks[i].price); I++; } }
C Language----program record