Hi, I can't seem to figure out where to go from here. I am trying to fill a one-dimensional array from the text file "AG_Midterms_Averaged.txt". How do i scan the text file and then store it into an array?
The text file is 40 lines of test scores like...
51.00
92.00
34.00
53.00...
Here is my code so far
Code:
#include <stdio.h>
#include <stdlib.h>
// Function Declarations
int getData (FILE* spDataIn, long int* studentId, int* score1, int* score2);
void midtermsAveraged (int score1, int score2, float* scoreAvg);
int writeMidtermsAvg (FILE* spDataOut, float scoreAvg);
int main (void)
{
// Local Declarations
FILE* spDataIn;
FILE* spDataOut;
long int studentId;
int score1;
int score2;
float scoreAvg;
// Statements
printf("Welcome...\n");
if (!(spDataIn = fopen("/Users/sincerevillanuevaiii/Documents/CIS15/HW8/AG_Midterm.txt", "r")))
{
printf("\aError opening employee file\n");
return 100;
}
if (!(spDataOut = fopen("AG_Midterms_Averaged.txt", "w")))
{
printf("\aError opening employee fileee\n");
return 102;
} // of open output
while (getData
(spDataIn, &studentId, &score1, &score2))
{
midtermsAveraged (score1, score2, &scoreAvg);
writeMidtermsAvg (spDataOut, scoreAvg);
} // while
fclose(spDataIn);
fclose(spDataOut);
printf("Done\n");
return 0;
} // main
/* ==============================getData=============================
Reads student data from text file.
Pre spDataIn is an open file
studentId, score1 and score2 are passed by address.
Post Reads student info
*/
int getData (FILE* spDataIn, long int* studentId, int* score1, int* score2)
{
// Local Declarations
int ioResult;
// Statements
ioResult = fscanf(spDataIn, "%ld %d %d", studentId, score1, score2);
if(ioResult == EOF)
return 0;
else
return 1;
} // getData
/* ==============================midtermsAveraged=============================
Determines average test scores based on two tests
Pre score1 and score 2 contrain the variables
Post scoreAvg copied to address
*/
void midtermsAveraged (int score1, int score2, float* scoreAvg)
{
// Statements
*scoreAvg = ((score1+score2)/2);
return;
} // midtermsAveraged
/*===============================writeMidtermsAvg===============================
Writes only the averaged score to the new text file.
Pre spDataOut is an open file
scoreAvg has values to write
Post Data written to file
*/
int writeMidtermsAvg (FILE* spDataOut, float scoreAvg)
{
// Statements
fprintf(spDataOut, "%.2f\n", scoreAvg);
return 0;
} // writeMidtermsAvg
Thanks