Sorry for the (probably simple) problem. I have some experience writing very simple science models in c++, matlab and R. I have been writing a new code and I can't seem to get the array to output to a file. The file created (using the ofstream option) has the header row but is otherwise blank, whereas the screen output (cout<<) I put in to troubleshoot the problem works fine and outputs to the debugger console. Alternately, I tried using the fwrite command, and that gives me a txt file with nonsense symbols. The code is pasted below:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
using namespace std;
const int maxtime = 2000;
float ENZc[3];
float Bc[3];
float EXc;
float EPc;
float ENZstore[maxtime];
float Bcstore[maxtime];
int main (int argc, char*const argv[])
{
int a, i, im1, t;
for (a=0; a<=2; a++)
{
ENZc[a]=1; // mgCg-1 soil
Bc[a]=30; // mgCg-1 soil
}
for (a=0; a<=20;a++)
{
ENZstore[a]=0;
Bcstore[a]=0;
}
//Define parameter values
float Kd = 1.03; //d-1
float Ke = 0.05;
float Kl = 0.05; //d-1
float Km = 0.022; // d-1
float SUE = 0.5; // gg-1
float Kes = 0.3; //mgCg-1 soil
float Re, Rm, Rg, ELc, EPc, Dc, RXc, Uc;
int dt = 1;
i=1;
im1=0;
for (t=0;t<maxtime; t++)
{
//Calculating modifiers
Dc = Kd*(ENZc[im1]/(Kes+ENZc[im1]));
Uc = Dc;
EPc = Ke*Uc; // Carbon for enzyme prod
Re=EPc*(1-SUE)/SUE; // Respiration for enzymer prod
Rm=Km*Bc[im1]; // Respiration for maintenance
ELc = Kl*ENZc[im1]; // enzyme loss rate
Rg=(Uc-EPc-Re-Rm)/(1-SUE); // Respiration for growth
RXc = 0;
ENZc[i]=dt*(EPc-ELc)+ENZc[im1];
Bc[i]=dt*(Uc-Re-Rm-Rg-EPc)+Bc[im1];
if (Bc[i]<0)
Bc[i]=0;
ENZstore[t]=ENZc[i];
Bcstore[t]=Bc[i];
i++;
im1++;
if (i>2)
i=0;
if (im1>2)
im1=0;
}
ofstream myfile;
myfile.open("/Users/Julie/Enzyme_noDiff/NoD_Test.txt");
myfile<<"row"<<" "<<"ENZc"<< " "<<"Bc"<<endl;
for (a=1750;a<2000;a++)
{
myfile<<a<<" "<<ENZstore[a]<<" " <<Bcstore[a]<<endl;
cout <<a<<" "<<" ENZC = "<<ENZstore[a]<<" BC = "<<Bcstore[a]<<"\n";
}
myfile.close();
FILE *fp;
fp=fopen("/Users/Julie/Enzyme_noDiff/NoD_Test2.txt","wb");
fwrite(ENZstore,sizeof(float),365,fp);
fclose(fp);
}
Last edited by a moderator: