2012-02-29 129 views
12

我正在試圖用Eigen庫學習C++。將矩陣寫入特徵文件?

int main(){ 
    MatrixXf m = MatrixXf::Random(30,3); 
    cout << "Here is the matrix m:\n" << m << endl; 
    cout << "m" << endl << colm(m) << endl; 
    return 0; 
} 

我怎樣才能出口m到一個文本文件(我已經搜查了單證 並沒有發現一個寫功能的提)?

回答

16

如果你可以把它寫在COUT,它適用於任何的std :: ostream的:

#include <fstream> 

int main() 
{ 
    std::ofstream file("test.txt"); 
    if (file.is_open()) 
    { 
    MatrixXf m = MatrixXf::Random(30,3); 
    file << "Here is the matrix m:\n" << m << '\n'; 
    file << "m" << '\n' << colm(m) << '\n'; 
    } 
} 
+2

是什麼科爾姆()該怎麼辦?它不適合我。 – Ludi 2016-01-01 12:22:51

0

我寫了這個功能:

void get_EigentoData(MatrixXf& src, char* pathAndName) 
    { 
      ofstream fichier(pathAndName, ios::out | ios::trunc); 
      if(fichier) // si l'ouverture a réussi 
      { 
      // instructions 
      fichier << "Here is the matrix src:\n" << src << "\n"; 
      fichier.close(); // on referme le fichier 
      } 
      else // sinon 
      { 
      cerr << "Erreur à l'ouverture !" << endl; 
      } 
    } 
+0

Merci,上面的答案也在工作...... :) – user189035 2012-07-10 15:28:39