Re: how to read tab delim file into 2D Array
On Mar 30, 11:29 am, yogi_bear_79 <yogi_bear...@yahoo.com> wrote:
Now, it's up to you to look at what everyone has said, decide how you wa=
nt
to try it, and try it, When you get stuck then ask how to fix your co=
de.
Getting Closer: This is building the vector, but it is one-
dimensional, seems to be a series of rows, versus columns. For
example the data is stored in d[0][0] - d[219][0]. Whenever I attempt
to increment col I get runtime errors. Also still need to figure out
how to use row to get to the next row in the vector, and finally I am
still losing my last column of data. Still Working on it though.
Tips needed!
int main()
{
ifstream file;
std::string line, value, delim = "\t";
typedef std::vector<std::vector<string> > Data;
Data d;
int row = 0, col = 0;
file.open ("test.txt",ios::in);
assert(file.is_open());
while (!file.eof()){
std::getline(file,line);
size_t startPos = 0, pos = line.find(delim);
if(row > 1)//skip title & header row
while (pos != std::string::npos){
value = line.substr(startPos, pos - startPos);
d.push_back(std::vector<string>(1));
d.back().at(col)=value;
startPos = pos + delim.length();
pos = line.find(delim, startPos);
}
row++;
col = 0;
}
file.close();