Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. // Author : Brandon Hawkins
  2. // Date : 9-12-18
  3. // File : program_004_social_security
  4. // Course : Computer Science 309
  5. // Instructor : Dr. Menon
  6. // Goal : Read from 3 different files, create special ID's
  7. // from social security numbers and prime squared.
  8. // Print the names, gpa and special ID's to new file
  9.  
  10. #define MAX_NAME_LENGTH 20
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. struct presidents {
  16. int soc_sec;
  17. int prime;
  18. float gpa;
  19. char firstName[MAX_NAME_LENGTH];
  20. char lastName[MAX_NAME_LENGTH];
  21. };
  22.  
  23. int main()
  24. {
  25. int i;
  26. struct presidents final[10];
  27. struct presidents temp;
  28. FILE * fp1;
  29. FILE * fp2;
  30. FILE * fp3;
  31. FILE * fp4;
  32.  
  33. //open file to read primes
  34. fp1 = fopen("10_primes.txt", "r");
  35. if (fp1 == NULL) {
  36. printf("\n\n Unable to open the file for reading... SORRY... Exiting the program");
  37.  
  38. return 1; // Error indicated with a return value of 1
  39. }
  40.  
  41. //read first file with primes and save them in struct array
  42. while(!(feof(fp1)))
  43. {
  44. for(i = 0; i < 10; i++)
  45. {
  46. fscanf(fp1, "%d", &temp.prime);
  47. final[i].prime = temp.prime;
  48. }
  49. }
  50. fclose(fp1);
  51.  
  52. //open file to read gpa
  53. fp2 = fopen("gpa.txt", "r");
  54. if (fp2 == NULL) {
  55. printf("\n\n Unable to open the file for reading... SORRY... Exiting the program");
  56.  
  57. return 1; // Error indicated with a return value of 1
  58. }
  59.  
  60. //read second files with GPA and save them in struct array
  61. while(!(feof(fp2)))
  62. {
  63. for(i = 0; i < 10; i++)
  64. {
  65. fscanf(fp2, "%f", &temp.gpa);
  66. final[i].gpa = temp.gpa;
  67. }
  68. }
  69. fclose(fp2);
  70.  
  71. //open file to read social security numbers
  72. fp3 = fopen("social_security.txt", "r");
  73. if (fp3 == NULL) {
  74. printf("\n\n Unable to open the file for reading... SORRY... Exiting the program");
  75.  
  76. return 1; // Error indicated with a return value of 1
  77. }
  78.  
  79. //read third file to save names and social security to struct array
  80. while(!(feof(fp3)))
  81. {
  82. for(i = 0; i < 10; i++)
  83. {
  84. fscanf(fp3, "%s %s %d", temp.firstName, temp.lastName, &temp.soc_sec);
  85. strcpy(final[i].firstName, temp.firstName);
  86. strcpy(final[i].lastName, temp.lastName);
  87. final[i].soc_sec = temp.soc_sec;
  88. }
  89. }
  90. fclose(fp3);
  91.  
  92. fp4 = fopen("finalList.txt", "w");
  93. if (fp4 == NULL) {
  94. printf("\n\n Unable to open the file for writing... SORRY... Exiting the program");
  95.  
  96. return 1; // Error indicated with a return value of 1
  97. }
  98.  
  99. for(i = 0; i < 10; i++)
  100. {
  101. fprintf(fp4, "%d %s %s %f", (final[i].soc_sec+(final[i].prime * final[i].prime)), final[i].firstName, final[i].lastName, final[i].gpa);
  102. }
  103. fclose(fp4);
  104.  
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement