Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jshoes

macrumors newbie
Original poster
Mar 14, 2011
3
0
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; // mgC•g-1 soil
		Bc[a]=30; // mgC•g-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; // g•g-1
	float Kes = 0.3; //mgC•g-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:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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
 

Attachments

  • NoD_Test.txt
    3.9 KB · Views: 83
Last edited by a moderator:

jshoes

macrumors newbie
Original poster
Mar 14, 2011
3
0
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

Moderator emeritus
Aug 16, 2005
19,366
979
New England
FWIW all I did was:

Code:
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

macrumors newbie
Original poster
Mar 14, 2011
3
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.