Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program #1 from Chapter 28 of
- // Absolute Beginner's Guide
- // to C, 3rd Edition
- /* The program takes the book info program
- from Chapter 27 and writes the info
- to a file named booksinfo.txt */
- // First include the file with
- // the structure definition
- #include <stdio.h>
- #include "bookinfo.h"
- #include <stdlib.h>
- FILE * filePointer;
- main(){
- // array of three structure variables
- struct bookInfo books[3];
- // Get the information about each book
- int i;
- for(i = 0; i< 3; i++){
- printf("\n\nWhat's the title of book number #%d?\n", i+1);
- gets(books[i].title);
- printf("\n\nWho's the author of that book?\n");
- gets(books[i].author);
- printf("\n\nHow many pages does it include?\n");
- scanf(" %d", &books[i].pages);
- printf("\n\nWhat's the price for that book?");
- scanf(" %f", &books[i].price);
- getchar(); // clears last newline for the loop
- }
- filePointer = fopen("C:\\Users\\user\\Documents\\booksInfo.txt", "w");
- if(filePointer == 0){
- printf("\nOhps, an error occurred");
- exit(1);
- }
- fprintf(filePointer,"\n\nHere is the collection of the books:\n");
- for(i =0; i<3; i++){
- fprintf(filePointer, "\n** Book number #%d - %s\n", i+1, books[i].title);
- fpirntf(filePointer, "\nWritten by %s, %d pages long", books[i].author, books[i].pages);
- fprintf(filePointer, "Market price: %f", books[i].price);
- }
- fclose(filePointer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement