我尝试从ODEINT库运行此示例以解决ODE。它运行得很好,但是我不想将结果显示在屏幕上,而是想将它们写到文件中。我将此ofstream添加到write_cout函数下的代码中,但它仅将结果的最后一行写入文件,而不是全部。您对此有任何想法吗?谢谢
#include <iostream>
#include <boost/numeric/odeint.hpp>
#include <fstream>
using namespace std;
using namespace boost::numeric::odeint;
void rhs( const double x , double &dxdt , const double t )
{
dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
}
void write_cout( const double &x , const double t )
{
cout << t << '\t' << x << endl;
cout<<"alo"<<endl;
ofstream buckyFile ("tuna.txt");
buckyFile<<t <<'\t'<<x<<endl;
}
// state_type = double
typedef runge_kutta_dopri5< double > stepper_type;
int main()
{
double x = 0.0;
integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) ,
rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
}
ofstream buckyFile ("tuna.txt");
tuna.txt
每次输入该功能都会打开一个新文件,从而覆盖以前的功能。
快速解决方法是使用
static ofstream buckyFile ("tuna.txt");
反而。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句