Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Brandon Hawkins
- // Date : 9-12-18
- // File : program_004_social_security
- // Course : Computer Science 309
- // Instructor : Dr. Menon
- // Goal : Read from 3 different files, create special ID's
- // from social security numbers and prime squared.
- // Print the names, gpa and special ID's to new file
- #define MAX_NAME_LENGTH 20
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct presidents {
- int soc_sec;
- int prime;
- float gpa;
- char firstName[MAX_NAME_LENGTH];
- char lastName[MAX_NAME_LENGTH];
- };
- int main()
- {
- int i;
- struct presidents final[10];
- struct presidents temp;
- FILE * fp1;
- FILE * fp2;
- FILE * fp3;
- FILE * fp4;
- //open file to read primes
- fp1 = fopen("10_primes.txt", "r");
- if (fp1 == NULL) {
- printf("\n\n Unable to open the file for reading... SORRY... Exiting the program");
- return 1; // Error indicated with a return value of 1
- }
- //read first file with primes and save them in struct array
- while(!(feof(fp1)))
- {
- for(i = 0; i < 10; i++)
- {
- fscanf(fp1, "%d", &temp.prime);
- final[i].prime = temp.prime;
- }
- }
- fclose(fp1);
- //open file to read gpa
- fp2 = fopen("gpa.txt", "r");
- if (fp2 == NULL) {
- printf("\n\n Unable to open the file for reading... SORRY... Exiting the program");
- return 1; // Error indicated with a return value of 1
- }
- //read second files with GPA and save them in struct array
- while(!(feof(fp2)))
- {
- for(i = 0; i < 10; i++)
- {
- fscanf(fp2, "%f", &temp.gpa);
- final[i].gpa = temp.gpa;
- }
- }
- fclose(fp2);
- //open file to read social security numbers
- fp3 = fopen("social_security.txt", "r");
- if (fp3 == NULL) {
- printf("\n\n Unable to open the file for reading... SORRY... Exiting the program");
- return 1; // Error indicated with a return value of 1
- }
- //read third file to save names and social security to struct array
- while(!(feof(fp3)))
- {
- for(i = 0; i < 10; i++)
- {
- fscanf(fp3, "%s %s %d", temp.firstName, temp.lastName, &temp.soc_sec);
- strcpy(final[i].firstName, temp.firstName);
- strcpy(final[i].lastName, temp.lastName);
- final[i].soc_sec = temp.soc_sec;
- }
- }
- fclose(fp3);
- fp4 = fopen("finalList.txt", "w");
- if (fp4 == NULL) {
- printf("\n\n Unable to open the file for writing... SORRY... Exiting the program");
- return 1; // Error indicated with a return value of 1
- }
- for(i = 0; i < 10; i++)
- {
- 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);
- }
- fclose(fp4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement