2. Create a script with array of structures to store Sailor Warrior's Name and their GPA. You need to type the name and GPA, then the script need to create a txt file based on the name and GPA score. Usagi Tsukino 1.98 Rei Hino 3.2 Amy Mizuno 4.0 Makoto Kino 2.9 Minako Aino 2.21 Haruka Tenou 3.5 Michiru Kaiou 3.78 (Do not ask how I remember all the names!)
C
Step by step
Solved in 4 steps with 3 images
I need the script to create a txt file thats made up of all the individual entries as well as the individual txt files for each entry.
The script pasted below prints individual entries but I need it to print 1 additional txt file thats like a sum of all the other created txt files. Thank you!
//script:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
#define MAX_WARRIORS 7
//Usagi Tsukino 1.98
//Rei Hino 3.2
//Amy Mizuno 4.0
//Makoto Kino 2.9
//Minako Aino 2.21
//Haruka Tenou 3.5
//Michiru Kaiou 3.78
struct SailorWarrior {
char name[MAX_NAME_LENGTH];
char name2[MAX_NAME_LENGTH];
float gpa;
};
int main() {
struct SailorWarrior warriors[MAX_WARRIORS];
char filename[MAX_NAME_LENGTH + 5]; // +5 for ".txt\0"
int i;
for (i = 0; i < MAX_WARRIORS; i++) {
printf("Enter the name and GPA for Sailor Warrior #%d: ", i+1);
scanf("%s %s %f", warriors[i].name,warriors[i].name2, &warriors[i].gpa);
sprintf(filename, "%s.txt", warriors[i].name); // create filename
FILE* fp = fopen(filename, "w"); // create file
if (fp == NULL) {
printf("Failed to create file for Sailor Warrior %s\n", warriors[i].name);
continue; // skip to next iteration of loop
}
fprintf(fp, "Name: %s %s\nGPA: %.2f\n", warriors[i].name,warriors[i].name2, warriors[i].gpa);
fclose(fp); // close file
}
return 0;
}