I have written a function that reads an unknown number of data (when in a column) from a file to a vector.
#include <iostream>
#include <vector>
#include <fstream> // file writing
#include <cassert>
void ReadFromFile(std::vector<double> &x, const std::string &file_name)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
size_t lineCount = 0;
while (!read_file.eof())
{
double temp;
read_file >> temp;
x.at(lineCount) = temp;
if (lineCount == x.size() - 1) { break; } // fixes the out of range exception
lineCount++;
}
read_file.close();
}
int main()
{
size_t Nx = 7;
size_t Ny = 7;
size_t Nz = 7;
size_t N = Nx*Ny*Nz;
// Initial Arrays
std::vector <double> rx(N);
std::string Loadrx = "Loadrx.txt";
ReadFromFile(rx, Loadrx);
}
But the lineCount is incrementing one extra time after the data from the file have been copied into the vector. Is there a more elegant way of fixing that problem than the if statement that I have written?