請問一下各位大神,遇到這樣一個問題該怎樣解決。
寫了一個類,有多個成員變數;
class A
{
double ex,ey,ez
double hx,hy,hz;
}
A有一個物件陣列 G[50][100];
有一個檔案夾下面有ex,ey,ez,hx,hy,hz的txt檔案每個檔案都是50*100的矩陣。
例如ex.txt檔案,里面是 1.. .... 99 100
. .... .
. .... .
50.........99 100
現在想把 ex,ey,ez,hx,hy,hz的內容匯入到G中,
比如想把ex匯入到 in>>G[50][100].ex中,ey匯入到G[50][100].ey中;
就是變數有點多,成員變數名字又不一樣,怎樣能回圈的把 不同檔案中的資料匯入到 物件陣列的不同成員變數 里去
uj5u.com熱心網友回復:
ifstream ifex("ex.txt");
ifstream ifey("ey.txt");
ifstream ifez("ez.txt");
string linex,liney,linez;
for(int i=0;i<50;i++)
{
getline(ifex,linex);
getline(ifey,liney);
getline(ifez,linez);
istringstream isex(linex);
istringstream isey(liney);
istringstream isez(linez);
for(int j=0;j<100;j++)
{
isex>>G[i][j].ex;
isey>>G[i][j].ey;
isez>>G[i][j].ez;
}
}
沒有錯誤判斷(比如檔案意外結束等),自己加入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/67653.html
標籤:基礎類
上一篇:為什么編譯沒錯,結果出不來
下一篇:如何求一個數的2的因子的個數
