The data file looks like this:
132.34
100
235
7859.03
444.31
332
298
545.77
676
33
I need to read this data in and then calculate the mean and variance of the data.
I'm just not sure which function in C is the most appropriate one to do read the data and store it in array 'value[m]'. I was thinking of using sscanf but I cant seem to get it to process the data correctly. Here's my code.
Code:
int main()
{
char file_name[100], line1[10] ;
float b1[10], value[10] ;
int count ;
int update_counter, m, prework2a = 0;
FILE *inp1 ;
// prompt the user
printf("Enter Filename : ") ;
// read the keyboard input
fgets(file_name, sizeof(file_name), stdin);
//remove the newline character from the filename
file_name[strlen(file_name) - 1] = '\0' ;
// open input file
inp1 = fopen(file_name, "r") ;
if(inp1 == NULL)
{
fprintf(stderr, "can't open %s\n", file_name) ;
exit(EXIT_FAILURE) ;
}
//read in the data from the file and store
//in array value[m]
for (m=1 ; m<=10 ; m++)
{
if(fgets(line1, sizeof(line1), inp1) != NULL) {
{
sscanf (line1, "%d",b1) ;
value[m] = b1[m] ;
printf("value = %d \n", value[m]) ;
}
}
fclose(inp1) ;
return 0 ;
}