C examples for File:File Write
Saving the Structure Contents in a File
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXTITL 40// w w w.j a v a2s .co m #define MAXAUTL 40 #define MAXBKS 10 /* maximum number of books */ char * s_gets(char * st, int n); struct book { /* set up book template */ char title[MAXTITL]; char author[MAXAUTL]; float value; }; int main(void) { struct book my[MAXBKS]; /* array of structures */ 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); /* go to start of file */ while (count < MAXBKS && fread(&my[count], size,1, pbooks) == 1) { if (count == 0) puts("Current contents of book.dat:"); printf("%s by %s: $%.2f\n",my[count].title, my[count].author, my[count].value); count++; } filecount = count; if (count == MAXBKS) { fputs("The book.dat file is full.", stderr); exit(2); } puts("Press [enter] at the start of a line to stop."); while (count < MAXBKS && s_gets(my[count].title, MAXTITL) != NULL && my[count].title[0] != '\0') { puts("Now enter the author."); s_gets(my[count].author, MAXAUTL); puts("Now enter the value."); scanf("%f", &my[count++].value); while (getchar() != '\n') continue; /* clear input line */ if (count < MAXBKS) puts("Enter the next title."); } if (count > 0) { puts("Here is the list of your books:"); for (index = 0; index < count; index++) printf("%s by %s: $%.2f\n",my[index].title, my[index].author, my[index].value); fwrite(&my[filecount], size, count - filecount, pbooks); } fclose(pbooks); return 0; } char * s_gets(char * st, int n) { char * ret_val; char * find; ret_val = fgets(st, n, stdin); if (ret_val) { find = strchr(st, '\n'); // look for newline if (find) // if the address is not NULL, *find = '\0'; // place a null character there else while (getchar() != '\n') continue; // dispose of rest of line } return ret_val; }