仅使用C ++从文本文件中读取同一行中特定搜索字符串后的字符串/字符/数据

时间:2015-05-26 11:32:10

标签: c++ string fstream seek

我有一个文本文件,内容是:

# 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)

现在我想获得Xmin Ymin Xmax Ymax值并在我的c ++程序中使用来将这些坐标与我新生成的坐标进行比较。那怎么做呢。我可以从文本文件中获取字符串“Xmax,Ymax”,但我不知道如何在此之后阅读以获得坐标。我的代码是serach string是:

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;
}

该程序在每一行中搜索字符串Xmax,Ymax,每当找到然后发现打印。但我想要这个字符串的指针,以便我可以增加指针值并读取Xmin的值然后跳过,然后读取Ymin的值然后跳过) - 并且读取Xmax的值并且对于Ymax读取相同的值。获得价值后转到下一行并做同样的事情。如果有人知道任何其他方法来读取这些值,那么请告诉我,我不需要X,Y的值。如果有任何错误,那么抱歉我是新来的。提前致谢

1 个答案:

答案 0 :(得分: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;        
    }
}