19

I have a std::istream which refers to matrix data, something like:

0.0 1.0 2.0
3.0 4.0 5.0

Now, in order to assess the number of columns I would like to have some code like:

std::vector<double> vec;
double x;
while( (...something...) && (istream >> x) )
{
    vec.push_back(x); 
}
//Here vec should contain 0.0, 1.0 and 2.0

where the ...something... part evaluates to false after I read 2.0 and istream at the point should be at 3.0 so that the next

istream >> x;

should set x equal to 3.0.

How would you achieve this result? I guess that the while condition

Thank you very much in advance for your help!

StephQ
  • 2,032
  • 3
  • 19
  • 27
  • Just to clarify, the reason I would like to pursue this approach (if doable) is to avoid creating temporary objects (apart from the double x). The matrix is then created by std::swap - ing the vector above with the matrix internal storage (it is implemented via a std::vector). – StephQ Jul 16 '10 at 09:12
  • well, you can do it with a temporary in 10 simple lines, or you can do it without the temporary in 50 hard lines. Use a temporary. – Mooing Duck Aug 28 '13 at 17:49

6 Answers6

14

Use the peek method to check the next character:

while ((istream.peek()!='\n') && (istream>>x))
tzaman
  • 46,925
  • 11
  • 90
  • 115
12

Read the lines into a std::string using std::getline(), then assign the string to a std::istringstream object, and extract the data from that rather than directly from istream.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Sorry I did not make this clear but this is what I am trying to avoid. Point is I do not know in advance if the matrix is going to have many cols or many rows and having string store 10.000 doubles is not a very efficient approch to the problem :) Sorry again for not making this clear, my fault. – StephQ Jul 16 '10 at 09:09
3
std::vector<double> vec;
{
   std::string line;
   std::getline( ifile, line );
   std::istringstream is(line);
   std::copy( std::istream_iterator<double>(is), std::istream_iterator<double>(),
              std::back_inserter(vec) );
}
std::cout << "Input has " << vec.size() << " columns." << std::endl;
std::cout << "Read values are: ";
std::copy( vec.begin(), vec.end(), 
           std::ostream_iterator<double>( std::cout, " " ) );
std::cout << std::endl;
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • OP specifically mentioned not wanting to buffer a whole line first. – tzaman Jul 16 '10 at 09:21
  • His proposed code requests that after the while the vector should contain the first line: `//Here vec should contain 0.0, 1.0 and 2.0`. Does he not want to read a line or the whole file? – David Rodríguez - dribeas Jul 16 '10 at 09:23
  • Yes I want to end the while loop after the first line has been read (and keep the rest in istream for further reading) but I would like to avoid having to create a string to store the first whole line before putting it in the vector. – StephQ Jul 16 '10 at 09:34
1

You can use std::istream::peek() to check if the next character is a newline. See this entry in the cplusplus.com reference.

MKroehnert
  • 3,637
  • 2
  • 34
  • 43
0

Read the number, then read one character to see if it's newline.

Messa
  • 24,321
  • 6
  • 68
  • 92
0

I had similar problem
Input is as below:

1 2
3 4 5

The 1st two were N1 and N2
Then there is a newline
then elements 3 4 5, i dont know how many these will be.

// read N1 & N2 using cin
int N1, N2;
cin >> N1;
cin >> N2;

// skip the new line which is after N2 (i.e; 2 value in 1st line)
cin.ignore(numeric_limits<streamsize>::max(), '\n');

// now read 3 4 5 elements
int ele;
// 2nd EOF condition may required,
//    depending on if you dont have last new-line, and it is end of file.
while ((cin_.peek() != '\n') && (cin_.peek() != EOF)) {
  cin >> ele;
  // do something with ele
}

This worked perfect for me.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140