I have a text file with this in it.
Smith,John,91,95,100
Jones,Mary,91,90,88
Simon,Simple,52,61,70
Clause,Kris,75,80,82
I have a Project8.h file with:
typedef struct{
char firstName[20];
char lastName[20];
int hw1;
int hw2;
int hw3;
}student;
and a Project8.c file with:
int main() {
int length = 20;
student grades[length];
readData( "scores.txt", grades, length );
printAveForHW( 2, grades, length );
printHighest( 3, grades, length );
return 0;
These I know are correct because they are given and I have to use the names.
The problem I'm trying to solve is this:
The readData function reads data from a file named filename and stores it into an array of students of length len. You may create an intermediate file as I did in my example to replace commas with spaces so that reading strings works as desired.
This is what I have but I'm completely lost. I changed the commas to spaces but I don't know what to do after this. I don't know how to read the data from a file named filename and store it into an array of students of length len.
#include "Project8.h"
void readData( char *filename, student arr[], int len );
int main(){
FILE *from, *to;
from = fopen("scores.txt", "r");
to = fopen("temp.txt", "w");
char c;
while(fscanf(from, "%c", &c) != EOF){
if(c != ',')
fprintf(to, "%c", c);
else
fprintf(to, " ");
}
fclose(from);
fclose(to);
void readData( char *filename, student arr[], int len )
FILE *filename = fopen("temp.txt", "r");
char lastName[20];
char firstName[20];
int hw1;
int hw2;
int hw3;
while(fscanf(filename, "%s %s %d %d %d", &lastName, &firstName, hw1, hw2, hw3)