Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 14, 2011, 09:36 AM   #1
jshoes
macrumors newbie
 
Join Date: Mar 2011
Problem outputting array to file in C++ on xcode

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 dejo; Mar 14, 2011 at 12:22 PM. Reason: Please use [code] tags.
jshoes is offline   0 Reply With Quote
Old Mar 14, 2011, 09:46 AM   #2
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
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
Attached Files
File Type: txt NoD_Test.txt (3.9 KB, 17 views)
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme

Last edited by dejo; Mar 14, 2011 at 12:22 PM. Reason: Cleanup.
balamw is offline   0 Reply With Quote
Old Mar 14, 2011, 10:15 AM   #3
jshoes
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
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.
jshoes is offline   0 Reply With Quote
Old Mar 14, 2011, 10:20 AM   #4
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
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
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Mar 14, 2011, 10:30 AM   #5
jshoes
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
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.
jshoes is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Expression Window Xcode 5 viewing dynamic array lights Mac Programming 0 Nov 5, 2013 08:06 PM
Xcode file output xcodeROOKIE Mac Programming 5 Oct 4, 2013 03:52 PM
C++ XCode v. 4.6 Shuffling an Array thrill4rishabh Mac Programming 25 Apr 26, 2013 12:05 AM
Reading from Text File into an Array in PHP Kilamite Web Design and Development 1 Aug 7, 2012 06:30 AM
Multiplying array elements by 3. C program on Xcode Mugambo Mac Programming 3 Jul 26, 2012 03:02 AM

Forum Jump

All times are GMT -5. The time now is 11:51 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC