Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 5, 2012, 10:11 PM   #1
grahamj1978
macrumors newbie
 
Join Date: May 2012
Xcode problem

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?
grahamj1978 is offline   0 Reply With Quote
Old May 6, 2012, 06:18 AM   #2
drsoong
macrumors member
 
Join Date: Mar 2008
Location: Munich
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.

-Drsoong
drsoong is offline   0 Reply With Quote
Old May 6, 2012, 10:14 AM   #3
vandersmissenc
macrumors newbie
 
Join Date: Sep 2011
Location: Houston, TX
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 ?
__________________
2009 Mac Pro 2.66 GHz Quad-Core, 16GB Ram, 640 GB & 160 GB HDD, XFX HD6870 1GB;
vandersmissenc is offline   0 Reply With Quote
Old May 6, 2012, 01:25 PM   #4
grahamj1978
Thread Starter
macrumors newbie
 
Join Date: May 2012
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.
grahamj1978 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
XCode 5 DP and Java Problem aquethys Mac Programming 0 Aug 28, 2013 09:59 PM
Xcode OpenGL Ram Problem jmfel1926 Mac Programming 5 Aug 21, 2013 06:06 PM
Problem with XCode 4 .6 Senor Cuete Mac Programming 7 Feb 11, 2013 09:15 AM
After updating XCode, output terminal problem iMacFarlane Mac Programming 9 Oct 3, 2012 11:29 PM
Xcode problem JamesP. Mac Applications and Mac App Store 0 Jul 4, 2012 10:41 AM

Forum Jump

All times are GMT -5. The time now is 01:03 PM.

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

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