PDA

View Full Version : Xcode problem




grahamj1978
May 5, 2012, 10:11 PM
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?



drsoong
May 6, 2012, 06:18 AM
Hi,

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


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:


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


g++ test.cpp -o test

Which works as expected:


$ 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

vandersmissenc
May 6, 2012, 10:14 AM
I am working in Xcode 4.3.2 and I used the exact code that drsoong posted

#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
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 ?

grahamj1978
May 6, 2012, 01:25 PM
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.