0

I have a text file and content is :

# Details for object 1 ("PASperson")
# Center point -- not available in other PASCAL databases -- refers
# to person head center
Original label for object 1 "PASperson" : "UprightPerson"
Center point on object 1 "PASperson" (X, Y) : (281, 169)
Bounding box for object 1 "PASperson" (Xmin, Ymin) - (Xmax, Ymax) : (250, 151) - (299, 294)

# Details for object 2 ("PASperson")
# Center point -- not available in other PASCAL databases -- refers
# to person head center
Original label for object 2 "PASperson" : "UprightPerson"
Center point on object 2 "PASperson" (X, Y) : (247, 373)
Bounding box for object 2 "PASperson" (Xmin, Ymin) - (Xmax, Ymax) : (215, 354) - (274, 466)

Now i want to get Xmin Ymin Xmax Ymax value and use in my c++ program to compare these coordinate with my newly generated coordinate. So how to do this. I can get string "Xmax, Ymax" from text file but i don't know how to read after that to get coordinate . My code is to serach string is:

int main()
    {
        std::string data;
        ifstream read_file("crop001501.txt",ios::in);
        char *search="Xmax, Ymax";
        int offset;
    while(std::getline(read_file ,data))
{
    //read_file>>data;
    //cout<<data<<endl;
    if ((offset = data.find(search, 0)) != string::npos) {
    cout << "found '" << search << endl;}
}
cin.get();
return 0;
}

This program is searching string Xmax,Ymax in every line and whenever found then printing found. but I want this string's pointer so that i can increase pointer value and read value of Xmin then skip , and read value of Ymin then skip ) - and read value of Xmax and same for Ymax . After getting value go to next line and do same. If anyone know any other method to read these value then please tell me , I don't need value of X,Y . If any mistake then sorry I am new here . Thanks in advance

Kaushal Joshi
  • 33
  • 1
  • 1
  • 7
  • 1
    When working line based in a text file. It is easiest and reusable to read in a line with getline() as in your code and then put the string in an istringstream so it can be parsed with std::cin >>. – stefaanv May 26 '15 at 11:47
  • I'd rather use regular expressions or a "real" parser (like boost::spirit) for this task. – m.s. May 26 '15 at 11:55
  • Thanks for reply @stefaanv , can you tell me how to get these coordinate , I could not understand your reply properly . Earlier I used cin >> also but i don't know how we can use it in this case. If possible can you show an example please . – Kaushal Joshi May 27 '15 at 03:25
  • thanks @m.s. , I don't know about boost::spirit , can you show some example how to extract/read coordinate from a specific location in text file – Kaushal Joshi May 27 '15 at 03:27

1 Answers1

0
#include <iostream>
#include <string>
#include <regex>
#include <cassert>
#include <chrono>
#include <sstream>

using namespace std;

int main(void){

    string line("Bounding box for object 1 \"PASperson\" (Xmin, Ymin) - (Xmax, Ymax) : (250, 151) - (299, 294)");

    // METHOD 1 - using regular expressions
    {
        regex e("object ([[:digit:]]+) \"PASperson\" \\(Xmin, Ymin\\) - \\(Xmax, Ymax\\) : \\(([[:d:]]+), ([[:d:]]+)\\) - \\(([[:d:]]+), ([[:d:]]+)\\)");

        smatch m;

        bool found = regex_search(line, m, e);

        if (found == true){
            assert(m.size() == 6);

            int object_id, xmin, xmax, ymin, ymax;

            object_id = stoi(m[1]);
            xmin = stoi(m[2]);
            ymin = stoi(m[3]);

            xmax = stoi(m[4]);
            ymax = stoi(m[5]);

            cout << "Object ID = " << object_id << endl;
            cout << "Xmin = " << xmin << endl;
            cout << "Ymin = " << ymin << endl;
            cout << "Xmax = " << xmax << endl;
            cout << "Ymax = " << ymax << endl;
        }
    }

    // METHOD 2 : using stringstream
    {
        istringstream ss(line);
        int object_id;
        string xmin_s, xmax_s, ymin_s, ymax_s;
        string other;
        ss >> other; ss >> other; ss >> other; ss >> other; // ignore 4 words
        ss >> object_id;
        ss >> other; ss >> other; ss >> other; ss >> other; ss >> other; ss >> other; ss >> other;  // ignore 7 words
        ss >> xmin_s; ss >> ymin_s; 
        ss >> other; // ignore -
        ss >> xmax_s; ss >> ymax_s;

        // get rid of leading/trailing symbols (,)
        xmin_s.erase(xmin_s.begin()); xmin_s.erase(xmin_s.end() - 1);
        xmax_s.erase(xmax_s.begin()); xmax_s.erase(xmax_s.end() - 1);
        ymin_s.erase(ymin_s.end() - 1);
        ymax_s.erase(ymax_s.end() - 1);

        int xmin, xmax, ymin, ymax;

        // convert strings to integers
        xmin = stoi(xmin_s);
        ymin = stoi(ymin_s);
        xmax = stoi(xmax_s);
        ymax = stoi(ymax_s);

        cout << "Object ID = " << object_id << endl;
        cout << "Xmin = " << xmin << endl;
        cout << "Ymin = " << ymin << endl;
        cout << "Xmax = " << xmax << endl;
        cout << "Ymax = " << ymax << endl;        
    }
}
911
  • 908
  • 8
  • 16
  • Thanks a ton brother... @911 . Appreciate your answer , its good . I also done by my self using substring . and then from substring i can read all coordinate and used it successfully . But my solution is not so efficient as yours . – Kaushal Joshi Jun 10 '15 at 11:08