I want to pull in column 2 (essentially after the first space) into an array. Nothing before or nothing after. BTW i'm not putting the full code, just trying to figure out how I would write the array.
Read each line.
You can use the good old strtok function http://www.cplusplus.com/reference/cstring/strtok/ and split the line on space character.
This creates an array for each line.
Get the element of your choice from the array.
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
int main()
{
ifstream fin("data.txt");
if (!fin)
{
cout << "could not open file" << endl;
return 1;
}
int one, two;
vector <int> array;
// read first two columns, ignore the rest of the line
while (fin >> one >> two)
{
array.push_back(two);
fin.ignore(1000, '\n');
}
// display the contents of the vector
for (int i=0; i<array.size(); i++)
{
cout << array[i] << endl;
}
return 0;
}