Hi all,

I am working on a data file that looks like the following:

value1: 0.7586 +/- 0.00473

value2: 0.664901 +/- 0.0357

value3: 0.662784 +/- 0.00447

-------------------------------------------------------------------------------------------------------------------------
value1: 0.765217 +/- 0.00425

value2: 0.694663 +/- 0.0277

value3: 0.66438 +/- 0.00393

-------------------------------------------------------------------------------------------------------------------------
value1: 0.758317 +/- 0.00332

value2: 0.648057 +/- 0.0201

value3: 0.660746 +/- 0.0035

-------------------------------------------------------------------------------------------------------------------------
value1: 0.767888 +/- 0.00442

value2: 0.67536 +/- 0.0228

value3: 0.669564 +/- 0.00418

------------------------------------------------------------------------------------------------------------------------

So it is 4 sets of value1 to 3 which are seperated by a dash line. I want to print out numbers in front of value1, value2 and value3 in each set. I started by writing a code that is finding the first value1 and tries to print its value. I wrote the following piss of code:

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int firstMatch(string w,string s,int startPos,int endPos){
        if(startPos==-1)return -1;
        int t[s.size()];
        int pos = 2;
        int cnd = 0;
        t[0] = -1; t[1] = 0;

        while (pos <w.size()){
                if (w[pos - 1] == w[cnd]){
                        t[pos] = cnd + 1; pos = pos + 1; cnd = cnd + 1;
                }
                else if (cnd > 0){
                        cnd = t[cnd];
                }
                else{
                        t[pos] = 0; pos = pos + 1;
                }
        }
        int m=startPos;
        int i=0;
        while (m + i < endPos){
                if (w[i] == s[m + i]){
                        i++;
                        if (i==w.size())
                                return m;
                }else{
                        m = m + i - t[i];
                        if (t[i] > -1)
                                i = t[i];
                }
        }
        return -1;
}
int firstMatch(string w,string s, int startPos){
        if(startPos==-1)return -1;

        return firstMatch(w,s,startPos,s.size());
}

int firstMatch(string w,string s){
        return firstMatch(w,s,0);
}
main(){
        string sentence,tmp;

        ifstream fin("file.xml");
	while(fin>>tmp)sentence+=tmp;

        int start = 0,end=0;
        while(start != -1){
                start = firstMatch("value1",sentence,start+1);
                int ind = start +8;
                end = firstMatch("\n",sentence,start+1);
		string t = "";
                for(int i=ind+1;i<end;i++){
                        if(sentence[i]>='0'&&sentence[i]<='9')
                                t+=sentence[i];
                        else
                          t+=' ';
                }

                if(start==-1)break;

                if(ind == -1)continue;

                istringstream iss(t);

                int a1,a2;
                iss>>a1>>a2;

                cerr<<a1<<'\t'<<a2<<'\t'<<endl;

        }
}

But using this code I a getting

0	4198195	
0	4198195	
0	4198195

Could you please help me with this.

Recommended Answers

All 2 Replies

I think you can accomplish this a little easier than what you are doing. If you read the whole string in all you have to do is find the ' '(space). Once you know that position delete everything in the string to that location. now you are at the first number. Then you find the first ' '(space). now you can copy the string from the start to the space into another string and then convert it to a number. Then search for the next space and delete everything in the string through the space. now the rest of the string is the number.

// using sentence as the string from the file
string temp;
size_t pos;
pos = sentence.find_first_of(" ", 0);
sentence.erase(0, pos + 1);
pos = sentence.find_first_of(" ", 0);
temp = sentence.substr(0, pos + 1);
//...

Something like that should get you the first value from the string. Should be able to figure the second half out.

Thanks for your reply. Using your suggestion I was able to solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.