I have a text file with one column of numbers. Eventually these numbers need to be added, averaged etc...but for right now I'm just trying to read them in, then print them out. My main problem is reading until EOF. Since I don't know how long the file will be...
Is there a way to fix this? Or a better way? Also, will I be able to do things like find the max value, min value, average etc using this format?
example of the text file:
43
23
746
543
76
2
654
....etc
Here's an example of the code I'm trying to write. This doesn't work properly though.
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *datafile;
int data[9999];
int x,s;
int z=0;
char path[80];
printf("Please enter the location of the file to be opened: ");
scanf("%s",&path);
datafile=fopen(path,"r");
if(datafile == NULL)
{
puts("Error opening file");
}
else
{
while(data != EOF)
{
fscanf(datafile,"%i",&data[s]);
s++;
z++;
}
puts("Data: ");
for(s=0;s<z;s++)
{
printf("%i\n",data[s]);
}
}
getch();
}