Counting number of entries in a txt file

So I have data that is being output to a txt file, with each piece of data being tabulated line by line (Each line has only 1 data entry).

I'm trying to store each of these pieces of data into an array for the purpose of taking a weighted average: http://en.wikipedia.org/wiki/Weighted_average
I'm basically trying to create a position correction algorithm.

The problem I'm having stems from the possibility that this text file from which i'm obtaining data can have a different number of entries each time I run this algorithm. So what I need to do in order to (1) Properly sum all the pieces of data in the file (in a for loop for example) and (2)take a weighted sum, is to count how many data entries are in the text file. I honestly have no clue as to how to do this... I've been trying some pretty weird stuff that hasn't been working.

Here's the significant bit of my code- I'm taking data from Pulse_area.txt and trying to store it in the A[i] array:

double correct_pos()
{
double A, x_avg, y_avg;

ifstream readfile;
readfile.open("Pulse_area.txt");

double A[number of entries in the text file];
while(!eof)
{
readfile >> A[i] >>endl;
i = i+1;
}

for (i = 0; i < num_entries ; i++)
{
A += A[i];
}

x_avg = x / A; //x will be passed into this function
y_avg = y / A; //same with y, don't worry about this stuff

return (x_avg, y_avg);
}

So i'm trying to figure out how to determine how 'i' should be limited in the for loop. I know there are other ways to sum up data entries in a file, but I feel this method is the most efficient for my purposes.

Thanks in advance!
Last edited on
Topic archived. No new replies allowed.