I'm having trouble in the while loop on how to get the string and only displaying the name major and GPA as a list. Also I need help in the fgets
function because each line is different every three lines so how would I display them in a list. What I would I put in the while
loop to make this work?
#include <stdio.h>
int main(void)
{
FILE *cfPtr;
FILE *ofp;
char name[20];
char major[3];
double gpa;
ofp = fopen("outputFile.txt","w");
fprintf(ofp,"Name\tMajor\tGPA\n");
if((cfPtr = fopen("inputFile.txt","r")) == NULL)
{
printf("This file could not be opened\n");
}
else
{
while (!feof(cfPtr))
{
fgets(name, 20, cfPtr );
//How to read the string and only display
//the name major and gpa?
//fprintf(ofp,"%s\t%s\t%f\n", name, major, gpa);
}
}
fclose(cfPtr);
fclose(ofp);
}
Input file is:
Name: John Milton
Major: EE
GPA: 3.98
Name: Karl Lewis
Major: CS
GPA: 3.6
Name: Homer Simpson
Major: CE
GPA: 4.0
and I need the output file as:
Name: Major: GPA:
John Milton EE 3.98
Karl Lewis CS 3.6
Homer Simpson CE 4.0