Reading in specific data-file via C++-Strings

Hey,

I'm feeling I've searched the whole net, without finding any solution to my problem...

I want to read in a data-file, where the data is formatted like:

1
2
3
4
14_229:	3.0000000000000000e+00	-2.0000000000000000e+00	7.0461538461538325e-05	1.6910769230769201e-04
14_233:	3.0000000000000000e+00	-2.0000000000000000e+00	7.1692307692307553e-05	1.7206153846153815e-04
14_237:	3.0000000000000000e+00	-2.0000000000000000e+00	7.2923076923076781e-05	1.7501538461538430e-04
14_241:	3.0000000000000000e+00	-2.0000000000000000e+00	7.4153846153846009e-05	1.7796923076923045e-04


Now, I want to extract the data from the line beginning with "14_237" and store the following values into double-variables.

My last assumption, was to read every line in to a string via getline, it contains in the beginning "14_237".

1
2
3
4
5
string para = "14_237";
for(int i = 0; str.substr(0,7).compare(para)!=0; ++i){
		getline(datastream,str);
		cout << str <<"	"<< str.substr(0,7).compare(para)<< endl;
	}


This works, but then the problem begins... I haven't found any possibility to read formatted data from a c++-string. I tried to convert the string to an array, to use "sscanf"... despite i think this is an ugly work-around, i didn't get this working...

therefore, i hope someone out there has a solution for me ;)

greetings
Something like this should do the job.

1
2
3
4
5
6
7
8
9
10
11
12
13
double w, x, y, z;
string a("");
ifstream inputStream("datafile");

do{
  inputStream >> a;
  inputStream >> w;
  inputStream >> x;
  inputStream >> y;
  inputStream >> z;
} while (a.find("14_237") == string::npos);

// At this point, we stopped looping because we found 14_237 in a, so w, x, y and z must be the values from that line 


A little more simple, but less adaptive:
1
2
3
4
5
6
7
8
9
10
11
12
13
double w, x, y, z;
string a("");
ifstream inputStream("datafile");

do{
  inputStream >> a;
  inputStream >> w;
  inputStream >> x;
  inputStream >> y;
  inputStream >> z;
} while (a != "14_237:");

// At this point, we stopped looping because we found 14_237: in a, so w, x, y and z must be the values from that line 
Last edited on
Bobu wrote:
I haven't found any possibility to read formatted data from a c++-string

that's what istringstreams are for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
int main()
{
    const std::string para = "14_237";
    std::ifstream f("test.txt");
    for(std::string line; getline(f, line); )
    {
        if(line.find(para) == 0)
        {
            std::cout << "Found the line that begins with " << para << '\n';
            std::istringstream linebuf(line);
            std::string word;
            double f1, f2, f3, f4;
            linebuf >> word >> f1 >> f2 >> f3 >> f4;
            std::cout << "The values were " << f1 << ", " << f2 << ", " << f3 <<
        }
    }
}
Found the line that begins with 14_237
The values were 3, -2, 7.29231e-05, 0.000175015

Thank you very much for your quick replies!! Everything works fine!! ;)
Topic archived. No new replies allowed.