Hi, I need to read a text file which has various lines containing integers. I need to write those integers separately in a vector. Example, the first line of the text file contains 3 9 8 7 6 so vector[4]=3, vector[3]=9, vector[2]=8 and so on. Next read the second line 4 1 2 3 4 5 and write to another vector vector[5]=4, vector[4]=1...
I tried the code below but it will write from the second line, the whole line in one vector index.
int str; // Temp string to
cout << "Read from a file!" << endl;
ifstream fin("functions.txt"); // Open it up!
string line;
// read line count from file; assuming it's the first line
getline( fin, line );
// convert the string to an integer
stringstream ss( line );
int n;
ss >> n;
// reserve vector size to line count
vector< string > function( n );
// read each line and put it into the vector in the reserved indicies
int i = 0;
while( i < n && getline( fin, line ) ) { //
function[i] = line;
i++;
}