My data is on a text file that contains two columns, x-values, and y-values. The number of data points on this file can be different but never exceeds 1000. So I have declared two arrays x[1000] and y[1000]. I have to read the data file and assign each number a specific variable so that I can use it later to do some calculation. Let's say, I have 319 data points in my text file:
x y 1 2.3 1.5 2.2 2.0 2.5 ... ... 160.0 35.5 Using my code below, I store the data in the following way:
x[0]=1, x[1]=1.5, x[2]=2.0, ............., x[318]=160.0 y[0]=2.3, y[1]=2.2, y[2]=2.5, ............., y[318]=35.5 Now I would like to count the number of elements that x is holding. In other words, I would like to know the size of my array x and/or y.
#include <iostream> #include <fstream> using namespace std; int main(){ int i=0; ifstream fin; fin.open("mydata.txt"); if (fin.fail()){ std::cerr << "Error opening file!" << endl; exit(1); } double x[1000], y[1000]; fin.ignore(1000,'\n'); while(!fin.eof()){ mufile >> x[i]; mufile >> y[i]; i++; } I tried:
int N=sizeof(x)/sizeof(*x); This gives me 1000, the size of the array that I declared in the beginning, (not 319 the size of the updated x).
codeblockstag is irrevelant...std::vector