![](/img/trans.png)
[英]Read data from file and sum over specific columns after hitting specific line (string) using 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.