繁体   English   中英

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

[英]read string/character/data after specific searched string in same line only from a text file using C++

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

# 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”,但是在此之后我不知道如何读取以获取坐标。 我的代码是要搜索字符串是:

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的值。 如果有任何错误,对不起,我是新来的。 提前致谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM