read integers from a file into a vector in C++

Posted by user2922063 on Stack Overflow See other posts from Stack Overflow or by user2922063
Published on 2013-10-26T03:14:39Z Indexed on 2013/10/26 3:54 UTC
Read the original article Hit count: 88

Filed under:
|

I am trying to read an unknown number of double values stored on separate lines from a text file into a vector called rainfall. My code won't compile; I am getting the error no match for 'operator>>' in 'inputFile >> rainfall' for the while loop line. I understand how to read in from a file into an array, but we are required to use vectors for this project and I'm not getting it. I appreciate any tips you can give on my partial code below.

vector<double> rainfall;    // a vector to hold rainfall data

// open file    
ifstream inputFile("/home/shared/data4.txt");

// test file open   
if (inputFile) {
    int count = 0;      // count number of items in the file

    // read the elements in the file into a vector  
    while ( inputFile >> rainfall ) {
        rainfall.push_back(count);
        ++count;
    }

    // close the file

© Stack Overflow or respective owner

Related posts about c++

Related posts about vector