2012-02-29 139 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