PDA

View Full Version : Problem outputting array to file in C++ on xcode




jshoes
Mar 14, 2011, 08:36 AM
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:

#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);
}



balamw
Mar 14, 2011, 08:46 AM
I relocated the file names to /tmp and it seems to work fine for me. (see attached file).

Could it be that you left the output file open in another process that is preventing your code from actually modifying it?

B

jshoes
Mar 14, 2011, 09:15 AM
That is interesting to know it works on your compiler. However, no, the problem persists even if I delete the .txt files or rename them, I still get one file (from the ofstream file) that has only the header names and the fwrite file has only miscellaneous symbols. If I modify the code to open the file as an ios:app file and run the code multiple times, I get multiple lines of header, but no numbers. It's odd, because I have similar code from an old model that works just fine. I checked this b/c I recently updated my Mac and, therefore, loaded the new version of Xcode so I wanted to make sure this wasn't the problem.

Thanks again for the advice.

balamw
Mar 14, 2011, 09:20 AM
FWIW all I did was:

cd ~/Desktop
nano deleteme.cpp
<COPY AND PASTE CODE FROM ABOVE, EDIT FILE NAMES, SAVE AND EXIT>
g++ ./deleteme.cpp
./a.out


You may want to try this outside Xcode to make sure it's not something funny in the IDE.

B

jshoes
Mar 14, 2011, 09:30 AM
Thanks. I will try that. I think it's really the last option that makes sense, after beating my head over code I was pretty sure _should_ work. Something in how Xcode is building the project must be causing the problem (or how I set up/designed the project when I opened it). My older simple models, when run, still successfully output to files, but they were written in the previous version of Xcode which had a different start-up process, so they must have carried over the correct configuration?

Thanks again for your help. I will call this problem solved.