的號碼,我有一個文本文件排列爲:從文本文件中讀取數據。我需要做兩件事情,第一我需要讀點
Diam
<D> 4.2
05:21:26 02:Apr:2012
Point 1
<X> 2
<Y> 5
<Z> 6
Point 2
<X> 4
<Y> 2
<Z> 0
Point 3
<X> 4
<Y> 1
<Z> 2
End
End
我需要做兩件事情,第一我需要閱讀的點數,所以基本上都看過多少線,點開始,爲了這個,我寫道:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//count number of data points in file (only count lines starting with P)
const char FileName[] = "myfile.txt";
int num = 0.;
string line;
ifstream file;
file.open (FileName);
while(file.eof() == false)
{
getline(file, line);
if (line[0]=='P') num++;
}
file.close();
return 0;
}
這給了我點的正確數量(雖然在代碼改進,歡迎),其次我需要閱讀的X,Y ,和z座標並忽略其他一切。 不知道如何去做這個,有什麼建議嗎?
在此先感謝!
編輯︰
謝謝大家的偉大的想法和答案! Obvlious船長提供了一個很好的答案,我想看看它是否可以適應一點。
所以,原來的.txt文件的格式爲:
Diam
<D> 4.2 5 6 4 2
05:21:26 02:Apr:2012
Point 1
<X> 2 5 6 4 2
<Y> 5 4 4 8 3
<Z> 6 7 6 0 2
Point 2
<X> 4 2 6 4 2
<Y> 2 3 5 8 4
<Z> 0 7 6 3 2
Point 3
<X> 4 0 6 2 2
<Y> 1 5 6 7 4
<Z> 2 0 6 5 3
End
End
同樣,我只需要值的第一列,其他的一切後,可以將其丟棄。我使用int
爲簡單起見,但數字將是double
。 宣讀後,我想將這些值在armadillo矩陣,所以我的矩陣的樣子:
cout << matrix << endl;
2 5 6
4 2 0
4 1 2
所以,我修改Obvlious船長的解決方案,以將數據放置在矩陣:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <sstream>
#include <stdexcept>
#include <armadillo>
using namespace std;
using namespace arma;
int main()
{
mat matrix(3,3); //here is where I set the size of the matrix,
//different files have different number of points,
//that's why I use my counting points code to set the size.
const char FileName[] = "myfile.txt";
string line;
ifstream file;
file.open (FileName);
for(;;)
{
getline(file, line);
if(file.eof()) {
break;
}
if (line.size() && line[0]=='P')
{
// Change "int" to "double" if necessary.
struct { double x, y, z; } data;
for(int i = 0; i < 3; i++)
{
getline(file, line);
if(line.size() > 3 && line[0] == '<' && line[2] == '>')
{
string::value_type pointElement = line[1];
// skip spaces and process data here to get the value
string::size_type offset = line.find(' ');
if(string::npos == offset)
{
throw invalid_argument("Invalid data format");
}
stringstream sline(line.substr(offset));
int value;
if(!(sline >> value))
{
throw invalid_argument("invalid data format");
}
switch(pointElement)
{
case 'X': data.x = value; break;
case 'Y': data.y = value; break;
case 'Z': data.z = value; break;
default:
// error in data format
throw invalid_argument("invalid data format");
}
}
else
{
// error in data format
throw invalid_argument("invalid data format");
}
}
// Do something with the values in data
cout
<< "point[x=" << data.x
<< ", y=" << data.y
<< ", z=" << data.z
<< "]" << endl;
// place data in matrix
// need to loop over k where k is the row of the matrix
matrix(k,0) = data.x; // format is matrix(rows,columns)
matrix(k,1) = data.y;
matrix(k,2) = data.z;
}
}
file.close();
return 0;
}
不知道在哪裏放置循環,將k設置爲矩陣的行來分配值。 對不起,對於超長篇帖子,並提前感謝您的任何幫助。在我看來,這是非常有用的東西,以備將來參考。
你可以使用awk嗎?它會簡化整個問題 – Nebril
@Nebril我不能:( – user1981855
)你顯然很熟悉使用'std :: getline()'(對於那個很好,btw)。跟隨你的點檢查有什麼問題嗎?三個'std :: getline()'調用,然後將這些行(一次一個)送入一個'istringstream'並提取標籤和值,填充一個點元組,並將它推入一個向量?(和注意:你會發現使用'file.eof()'作爲循環終止符不會像你想象的那樣工作)。 – WhozCraig