我正在尝试使用 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
到文本文件(我已经搜索了文档,但没有发现提及写作功能)?
如果你可以在 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';
}
}
我写了这个函数:
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;
}
}