Advertisement
Guest User

Untitled

a guest
May 27th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. // Example program #1 from Chapter 28 of
  2. // Absolute Beginner's Guide
  3. // to C, 3rd Edition
  4.  
  5. /* The program takes the book info program
  6. from Chapter 27 and writes the info
  7. to a file named booksinfo.txt */
  8.  
  9. // First include the file with
  10. // the structure definition
  11.  
  12. #include <stdio.h>
  13.  
  14. #include "bookinfo.h"
  15.  
  16. #include <stdlib.h>
  17.  
  18. FILE * filePointer;
  19.  
  20. main(){
  21.  
  22. // array of three structure variables
  23. struct bookInfo books[3];
  24.  
  25. // Get the information about each book
  26. int i;
  27. for(i = 0; i< 3; i++){
  28. printf("\n\nWhat's the title of book number #%d?\n", i+1);
  29. gets(books[i].title);
  30.  
  31. printf("\n\nWho's the author of that book?\n");
  32. gets(books[i].author);
  33.  
  34. printf("\n\nHow many pages does it include?\n");
  35. scanf(" %d", &books[i].pages);
  36.  
  37. printf("\n\nWhat's the price for that book?");
  38. scanf(" %f", &books[i].price);
  39.  
  40. getchar(); // clears last newline for the loop
  41. }
  42.  
  43. filePointer = fopen("C:\\Users\\user\\Documents\\booksInfo.txt", "w");
  44.  
  45. if(filePointer == 0){
  46. printf("\nOhps, an error occurred");
  47. exit(1);
  48. }
  49.  
  50. fprintf(filePointer,"\n\nHere is the collection of the books:\n");
  51. for(i =0; i<3; i++){
  52.  
  53. fprintf(filePointer, "\n** Book number #%d - %s\n", i+1, books[i].title);
  54.  
  55. fpirntf(filePointer, "\nWritten by %s, %d pages long", books[i].author, books[i].pages);
  56.  
  57. fprintf(filePointer, "Market price: %f", books[i].price);
  58. }
  59.  
  60. fclose(filePointer);
  61.  
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement