Read numbers from stream

Hi

Can any one help here I am trying to pull out two values from
this data stream

// IDE Code::Blocks C++ Feb 2011
//
// File mattias_s.txt
//
// #G01,X 106.0332,Y 234.7669, , ,
// #G01,X 110.2210,Y 232.1646, , ,
// #G01,X 111.6001,Y 230.2080, , ,
// #G01,X 111.6001,Y 228.5585, , ,

[code]

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
string str; // Set up string str
string str2, str3; // Subdivide str

ifstream myfile ("c:\\bust\\bust_samples\\mattias_s.txt");// Get file input
if (myfile.is_open())
{
while (! myfile.eof() )// Read to end of file
{
getline (myfile, str);// Get each line from myfile and put it in str

str2 = str.substr (7, 8); // str2 = value @ 7 in for 8 characters
str3 = str.substr (18, 8); // str3 = value @ 18 in for 8 characters

}
cout << str2 << ' ' << str3 <<"\n";// output to screen
}

return 0;
}

// Output to screen is = 111.6001 228.5585
//
// 106.0332 234.7669
// 110.2210 232.1646
// 111.6001 230.2080
// 111.6001 228.5585
// This is what I want all the values from the stream.
// How can I get this output ? string array ?

[code/]
I'm not clear on what the problem is, to my mind, haven't stated it clearly.

If all you're missing is the output, move the cout statement into the while loop where the values str2 and str3 are populated.
Last edited on
 
cout << str2 << ' ' << str3 <<"\n";// output to screen 

The cout which preform the output is located outside the loop which gets lines from the file. So you will only print the last line which was read from the file.

1
2
3
4
5
6
7
8
while (! myfile.eof() )// Read to end of file
{
  getline (myfile, str);// Get each line from myfile and put it in str

   str2 = str.substr (7, 8); // str2 = value @ 7 in for 8 characters
   str3 = str.substr (18, 8); // str3 = value @ 18 in for 8 characters
   cout << str2 << ' ' << str3 <<"\n";// output to screen
}

It would probably work correctly if the cout line was located in the loop =)
Hi

you where right, works correct now, my brain went dead.

thanks, I owe you both a beer.

Peter
Individual Bust
Topic archived. No new replies allowed.