C Language----program record

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.