If I getline, each I only have 3 cells.
If I "infile >>" I will have 9 cells.
What I want to do:
The lines are strings. I want to convert each numbers in to integers.
If I use "infile >>" I need to know when is the end of the line and start of the line.
Not every line is going to have 3 numbers, it could have more or less.
If I use getline, the whole line is in a single cell so I don't know how I can convert each numbers.
/*
* Convert a text file lines into integers
* Convert each line into integers and store them into a int vector
* Now I just have to make the conversion mechanism shorter....
*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
usingnamespace std;
int main () {
ifstream i("example.txt");
vector<string> v;
string line;
while(i.good())
{
getline(i, line);
v.push_back(line);
}
string x, z = "";
char kind;
int y = 0, w = 0, u;
vector<int> vv;
for(int i = 0; i < v.size();i++)
{
x = v[i];
for(int j = 0;j < x.length();j++)
{
kind = x[j];
if(kind == ' ')
{
y = j;
while(w < y)
{
z += x[w];
w++;
}
u = atoi(z.c_str());
vv.push_back(u);
z.clear();
}
}
w = 0;
}
for(int i = 0;i < vv.size();i++)
{
cout << vv[i] << endl;
}
return 0;
}
If you want to test it out
At the end of each line put a space.
This will be nearly as impossible if you are working for a company. So you need to add a space using code instead of manually putting a space by opening up the text file.