Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. /* multiprez.c - Write Information About Multiple Presidents
  2. *
  3. * D Provine
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9.  
  10. // Obama is #44
  11. #define MAXENTRIES 44
  12.  
  13. typedef struct {
  14. char inits[5];
  15. unsigned char num;
  16. } prezinfo;
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20. prezinfo person[MAXENTRIES];
  21. FILE *saveit;
  22. int numscanned;
  23. int num;
  24. char inits[5];
  25. int thispres;
  26. int i;
  27.  
  28. if (argc != 2) {
  29. fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
  30. exit(1);
  31. }
  32.  
  33. // NOTE: NO ERROR CHECKING ON INITIALS!
  34. thispres = 0;
  35. while ( ( numscanned = scanf("%d", &num) ) != EOF && num != 0 ) {
  36. if (numscanned == 1) {
  37. // get the initals
  38. numscanned = scanf("%s", &inits[0]);
  39.  
  40. person[thispres].num = num;
  41. strcpy(person[thispres].inits, inits);
  42.  
  43. thispres++;
  44. }
  45. if (thispres > MAXENTRIES)
  46. break;
  47. }
  48.  
  49. if ( ( saveit = fopen(argv[1], "w") ) == NULL ) {
  50. perror("pres.dat");
  51. exit(1);
  52. }
  53.  
  54.  
  55. for (i = 0; i < thispres; i++) {
  56. printf("%d\n", fwrite(&(person[i]), sizeof(prezinfo), 1, saveit));
  57. }
  58.  
  59. // NOTE: an alternate way to do this could have been
  60. // printf("%d\n", fwrite(person, sizeof(prezinfo), thispres-1, saveit));
  61. // which would have written all of them at once.
  62. // The printf() would have printed however many were there.
  63. // This one will print "1" for each president.
  64.  
  65. fclose(saveit);
  66.  
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement