ofstream不会将不同的数据写入两个不同的文件

时间:2017-01-16 11:05:09

标签: c++ ofstream

我无法使用ofstream一次将两个不同的输出写入两个不同的文件。该程序编译并运行正常,并将数据写入p1output.txt,但是当我打开p2output.txt时,除了包含内容的第一行外,它是空白的:

Time x1 x2 v1 v2 Energy Angular Momentum

如果删除将数据写入p1output.txt的代码行,程序会将数据正确写入p2output.txt。我唯一能想到的是,一个接一个地使用两个Leapfrog函数可以防止第二个函数执行并打印出数据。我不知道为什么会这样或如何解决它 - 有人可以帮忙吗?

以下是代码:

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

#define D 2  // number of dimensions
struct particle
{
    double x[D] ; // (x,y) coordinates
    double v[D] ; // velocity
    double F[D] ; // Force
    double a[D] ; // acceleration
    double GMm ;  // gravitational parameter
    double im ; // inverse mass
    double PE    ; // potential energy
    double T ; // kinetic energy
    double E ; // total energy
    double J; // angular momentum
    double r    ; // distance from origin
};


void accel(particle &p_n) // acceleration of particle
{
    for (int i=0; i<D; i++ )
    {
        p_n.a[i]=p_n.x[i]/(p_n.r*p_n.r*p_n.r) ;
    }
}


void ForceEnergy(particle &p_n) //force and energy of particle
{
    double R=0.0;

    for(int i=0; i<D; i++)
    {
        R+=p_n.x[i]*p_n.x[i];
    }

    double r=sqrt(R);

    p_n.PE=-p_n.GMm/r ; // PE of particle
    p_n.r=r; // absolute distance of particle from origin

    for(int i=0; i<D; i++)
    {
        p_n.F[i]=-p_n.GMm*p_n.x[i]/(p_n.r*p_n.r) ;
    }

    p_n.T=0.0;

    for(int i=0; i<D; i++)
    {
        p_n.T+=0.5*p_n.v[i]*p_n.v[i]/p_n.im;
    }

    p_n.E=p_n.T+p_n.PE;

}

void VectorProduct(double x[],
                   double y[],
                   double z[],
                   int N) // finding the cross product of 2 vectors
{
    double d[3], e[3];

    for(int i=0; i<3; i++)
    {
        d[i]=0;
        e[i]=0;
    }

    for(int i=0; i<N; i++)
    {
        d[i]=x[i];
        e[i]=y[i];
    }

    z[0]=d[1]*e[2]-d[2]*e[1];
    z[1]=d[2]*e[0]-d[0]*e[2];
    z[2]=d[0]*e[1]-d[1]*e[0];
}

double VectorMag(double u[] ,
                 int n) // calculates magnitude of a vector
{
    double vm=0;

    for(int i=0; i<n; i++)
    {
        vm=vm+u[i]*u[i];
    }

    return sqrt(vm);
}

void AngMom(particle &p_n) // finding angular momentum about the origin
{
    double z[3];

    VectorProduct(p_n.x,
                  p_n.v,
                  z,
                  D);

    p_n.J=VectorMag(z,3)/p_n.im;
}

void xchange(particle &p_n ,
             double dt) // position increment
{
    for(int i=0; i<D; i++)
    {
        p_n.x[i]+=dt*p_n.v[i] ;
    }
}

void vchange(particle &p_n ,
             double dt) // momentum increment
{
    for(int i=0; i<D; i++)
    {
        p_n.v[i]+=dt*p_n.a[i] ;
    }
}

void ParticleState(particle p_n ,
                   double t,
                   const char filename[]) // printing out the particle state
{
    ofstream fxout;

    fxout.open(filename,
               ios::app);
    if(fxout.good()==false)
    {
        cerr << "can't write to file " << filename << endl;
        exit(0);
    }
    else
    {
        fxout << t << "\t" << p_n.x[0] << "\t" << p_n.x[1] << "\t"<< p_n.v[0] << "\t" << p_n.v[1] << "\t"<< p_n.E << "\t"<< p_n.J << endl;

        fxout.close();
    }

}

void Leapfrog(particle &p_n,
              double dt,
              double &t,
              int N,
              const char filename[])
{
    while(t < N)
    {
        ParticleState(p_n,
                      t,
                      filename);
        xchange(p_n,
                dt*0.5); // position moved by a half-step
        t+=0.5*dt;
        accel(p_n); // computes acceleration at this position
        vchange(p_n,
                dt); // velocity moved by a full-step
        xchange(p_n,
                dt*0.5) ; // position moved by another half-step
        t+=0.5*dt ;
    }
}

int main()
{
    particle p_1, p_2;
    double dt=0.01; // time per step
    double t=0.0; // start time
    int N=1000;  // number of time steps

    p_1.im=0.02;    p_2.im=0.5;
    p_1.v[0]=0;     p_2.v[0]=1;
    p_1.v[1]=20;    p_2.v[1]=20;
    p_1.x[0]=4;     p_2.x[0]=4;
    p_1.x[1]=4;     p_2.x[1]=4;

    p_1.GMm=100;
    p_2.GMm=100;

    ForceEnergy(p_1);
    accel(p_1);
    AngMom(p_1);

    ForceEnergy(p_2);
    accel(p_2);
    AngMom(p_2);

    ofstream f1out;

    f1out.open("p1output.txt");
    f1out << "#Time" << "\t" << "x1" << "\t" << "x2" << "\t" << "v1" << "\t" << "v2" << "\t" << "Energy" << "\t" << "Angular Momentum" << endl;
    f1out.close();

    ofstream f2out;

    f2out.open("p2output.txt");
    f2out << "#Time" << "\t" << "x1" << "\t" << "x2" << "\t" << "v1" << "\t" <<"v2" << "\t" << "Energy" << "\t" << "Angular Momentum" << endl;
    f2out.close();

    Leapfrog(p_1,
             dt,
             t,
             N,
             "p1output.txt");

    Leapfrog(p_2,
             dt,
             t,
             N,
             "p2output.txt");

    return 0;
}

1 个答案:

答案 0 :(得分:1)

enter image description here

您通过引用取“t”。因此,当您为p2output.txt写入参数时,“t”已经大于“N”。因此它不会进入while(t