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

grahamj1978

macrumors newbie
Original poster
May 5, 2012
2
0
I just recent started working with Xcode 4 and I'm trying to update some code that I originally ported from Matlab to Xcode 3.something. The problem I'm having is that when the code looks for a parameter file and finds that it's not there it should create a default file. However, I'm just getting a blank file.

The problem seems to resolve around file i/o, but I can't figure it out. I created a test that helped partially illuminate the problem with the following code:

int fred;
fred=126;
ofstream myFile("thisfile.txt");
myFile << "This is my message: ";
myFile << "bob" << endl;
myFile << "fred";
myFile << fred << 125 << "bob" << endl;
myFile.close();

I received this as output:

This is my message: bob
fred

Note that none of the numerical values showed up. If I move the 'fred' variable earlier in the steam the latter text doesn't show up either. It seems that as soon as a non-string value is introduced to the stream that it stops working. This was certainly not in issue in Xcode3, and I'm at a loss as to how to fix the problem I'm having. I don't want to output strings, I want to be able to output numbers as I used to.

Any ideas?
 
strange, your code works compiled in the terminal

Hi,

I don't see any obvious error in your code. My only suggestion would be to insert a

Code:
myFile.flush();

before the close(). Normally, close() should, yet, flush all pending output buffers to disk anyway.

I copy and pasted your exact code, adding the necessary main-bla etc., into a file "test.cpp" and compiled it in the terminal:

Code:
#include <fstream>

using namespace std;

int main(void)
{
   	int fred;
	fred=126;
	ofstream myFile("thisfile.txt");
	myFile << "This is my message: ";
	myFile << "bob" << endl;
	myFile << "fred";
	myFile << fred << 125 << "bob" << endl;
	myFile.close();

	return 0;
}

Code:
g++ test.cpp -o test

Which works as expected:

Code:
$ cat thisfile.txt 
This is my message: bob
fred126125bob

This could be a strange XCode issue. You could try still compiling it in XCode, but running it in the terminal, maybe it is just an output issue in the XCode-integrated terminal. :confused:

-Drsoong
 
4.3.2

I am working in Xcode 4.3.2 and I used the exact code that drsoong posted

Code:
#include <fstream>

using namespace std;

int main(void)
{
   	int fred;
	fred=126;
	ofstream myFile("thisfile.txt");
	myFile << "This is my message: ";
	myFile << "bob" << endl;
	myFile << "fred";
	myFile << fred << 125 << "bob" << endl;
	myFile.close();
    
	return 0;
}

when I compiled and ran it in Xcode the output was as expected
Code:
Carls-Mac-Pro:~ vandersmissenc$ cat thisfile.txt 
This is my message: bob
fred126125bob

so to me it seems this is possibly an issue with either the version of Xcode you are using or the hardware.

Which version of Xcode are you running ?
 
4.2

I'm using 4.2 / 4.3.2 - I've been alternating between a 10.6 desktop Hackintosh and my 10.7 laptop.

After looking at the results you both got, I decided to try recreating my project fresh in Xcode4 and just adding the source files. And amazingly, it now works fine. I assume the problem originated from the conversion to Xcode4 from the earlier version I had been using. Although, I have no idea what the problem may have been.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.