3

I have an text file with binary values in n columns and y rows.

I am using getline to extract every row of the binary values and assign them to vectors:
I am using getline to extract every row of a file, where each row consists of a series of '0' or '1' separated by space, and assign them to a vector.

std::vector< std::vector<int> > matrix; // to hold everything.
std::string line;
while(std::getline(file,line))
{
  std::stringstream linestream(line);
  int  a,b,c,d;
  linestream >> a >> sep >> b >> sep >> c >> sep >> d;
  std::vector <int> vi;
  vi.push_back(a);
  vi.push_back(b);
  vi.push_back(c);
  vi.push_back(d);
  matrix.push_back(vi);
}

Now the problem is that I do not know in advance how many columns are there in the file. How can I loop through every line until i reach the end of that line?

Martin York
  • 257,169
  • 86
  • 333
  • 562
alandalusi
  • 1,145
  • 4
  • 18
  • 39

3 Answers3

3

The obvious way would be something like:

while (linestream >> temp >> sep) 
    vi.push_back(temp);

Though this may well fail for the last item, which may not be followed by a separator. You have a couple of choices to handle that properly. One would be the typical "loop and a half" idiom. Another would be a locale that treats your separator characters as white space.

When/if you do that, you can/could also use a standard algorithm:

std::copy(std::istream_iterator<int>(linestream),
          std::istream_iterator<int>(),
          std::back_inserter(vi));
Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 3
    So fix it by separating the reads: `while (linestream >> temp) { vi.push_back(temp); linestream >> sep; }` – Josh Jun 29 '11 at 18:49
  • 1
    @user349433: That's certainly possible too -- but I prefer to interpret its separators as white space. – Jerry Coffin Jun 29 '11 at 18:53
  • Didn't know about the _loop and a half_ ideom, now found a link: [loop and a half](http://www.cs.duke.edu/~ola/patterns/plopd/loops.html#loop-and-a-half) – Christian Ammer Jun 29 '11 at 18:57
  • I'm asking for trouble here, but what if the separator is more than one character? Ideally, and most likely, the chars that make up the separator are not numeric, so it wouldn't apply here. But, I'm curious as to how a multi-char separator would be implemented in terms of a locale. – rcollyer Jun 29 '11 at 19:01
  • @rcollyer: That depends. It's easy to treat all of a number of characters as white space. If (for example) you need to verify a specific string of characters in the right order, that approach doesn't work (for that, look to TR1/Boost regex). – Jerry Coffin Jun 29 '11 at 19:11
0

Why not while (in1 >> i) row.push_back( i ); Which does not require a separator?

John
  • 2,326
  • 1
  • 19
  • 25
-1

check for a new line character (\n). when you find one, you've completed the line/column.

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • Neither is your comment. How about you explain how it isn't helpful instead of bewildering me? – MGZero Jun 29 '11 at 19:11
  • Well, he's already reading one line at a time. So searching for a newline does nothing. In fact, [there won't even be one there](http://www.cplusplus.com/reference/string/getline/). The issue he's having is with iterating through columns and not knowing how many times to run his "checks"; suggesting a new "check" does not have anything to do with solving the stated problem. – Lightness Races in Orbit Jun 29 '11 at 19:16
  • I see, I misunderstood OP's question then. – MGZero Jun 29 '11 at 19:17