Search code examples
c++fstream

Function reading from a file into vector C++


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?


Solution

  • I have written a function that reads an unknown number of data (when in a column) from a file to a vector.

    One of the most elegant (and, I suppose, idiomatic) ways to read unknown amount of data from a "column" (or otherwise regularly-formatted) file is to use istream iterators:

    void ReadFromFile(std::vector<double> &x, const std::string &file_name)
    {
        std::ifstream read_file(file_name);
        assert(read_file.is_open());
    
        std::copy(std::istream_iterator<double>(read_file), std::istream_iterator<double>(),
            std::back_inserter(x));
    
        read_file.close();
    }
    

    Usage:

    int main()
    {
        // Note the default constructor - we are constructing an initially empty vector.
        std::vector<double> rx;
        ReadFromFile(rx, "Loadrx.txt");
    }
    

    If you want to write a "safe" version with a limited number of elements to read, use copy_if:

    void ReadFromFile(std::vector<double> &x, const std::string &file_name, unsigned int max_read)
    {
        std::ifstream read_file(file_name);
        assert(read_file.is_open());
    
        unsigned int cur = 0;
        std::copy_if(std::istream_iterator<double>(read_file), std::istream_iterator<double>(),
        std::back_inserter(x), [&](const double&) {
            return (cur++ < max_read);
        });
    
        read_file.close();
    }
    

    Usage is obvious:

    ReadFromFile(rx, Loadrx, max_numbers);