I have a simple function of the form:
double f(double x)
{
...
}
For that function, I want to use data from a txt file, which I pass to an array:
ifstream inFile;
inFile.open("data.txt");
//Counting lines
string s;
int nlines=0;
while(!inFile.eof()){
getline(inFile, s);
nlines++;
}
inFile.seekg(0, ios::beg);
while(!inFile.eof()){
inFile >> a[entry_data];
inFile >> b[entry_data++];
}
inFile.close();
If I put this code inside the function f
, each time the function is called, it will have to open the file, create the array... and so on. I want to avoid this by defining the array just the first time the function is called, or before. How can I do this?
I know that if I define the array in the main scope and pass it to the function as an argument I can solve this, but I want to keep the main scope as clean as possible. Thanks!