tags:

views:

91

answers:

4

My test file has data like this:

1
2
3
0
1, 2
3, 4
0, 0
4, 3
2, 1
0, 0

How would I separate the data by line but also separate each section of data by the zeros.

 ifstream data("testData.txt");
string line, a, b;


while(getline(data,line))
{
    stringstream str(line);
 istringstream ins;
 ins.str(line);
 ins >> a >> b;

 hold.push_back(a);
 hold.push_back(b); 
}

How do I separate them by the zeros?

+3  A: 

First of all, I would try to improve the problem definition ☺

Ariel
A: 

Define your input file better and the problem will be easier :)

Polaris878
A: 

When you're done you have

[1,2,3,0,1,2,3,4,0,0,4,3,2,1,0,0]

How about using std::find()?

wilhelmtell
+1  A: 

So the lines are significant, and the zero-delimited lists of numbers are also significant? Try something like this:

std::ifstream data("testData.txt");
std::vector<int> hold;
std::string line;
std::vector<std::string> lines;

while(std::getline(data,line))
{
  lines.push_back(line);
  std::stringstream str(line);

  // Read an int and the next character as long as there is one
  while (str.good())
  {
    int val;
    char c;
    str >> val >> c;
    if (val == 0)
    {
      do_something(hold);
      hold.clear();
    }
    else
      hold.push_back(val);
  }
}

This isn't very fault-tolerant, but it works. It relies on a single character (a comma) to be present after every number except the last one on each line.

Darryl