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 Sep 5, 2009, 07:12 PM   #1
CharlesRussell3
macrumors newbie
 
Join Date: Sep 2009
C++ in Xcode

Howdy!

I've programmed in C++ on Windows for nearly 2 years. I recently switched to a Mac, and started using Xcode.
Then I come across a little snag, when I started writing a simple test program to spit me a numerical values on a txt/dat file.

#include <iostream>
#include <fstream>

using namespace std;

int main (int argc, char * const argv[]) {
fstream fout("test.dat", ios::out);

double a = 1;
double b = 0;

fout << "Hello World!\n" << endl;
fout << a << '\t' << b << '\t' << a+b << '\t' << a-b << endl;

}

The program successfully complied, as it should. When I opened test.dat, I do get the line "Hello World!"; however, the numerical values of a, b, a+b, and a-b does not appear. Any suggestions? Thank you.

Cheers,
Charles
CharlesRussell3 is offline   0 Reply With Quote
Old Sep 5, 2009, 09:22 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by CharlesRussell3 View Post
The program successfully complied, as it should. When I opened test.dat, I do get the line "Hello World!"; however, the numerical values of a, b, a+b, and a-b does not appear. Any suggestions?
Post the version of the OS and Xcode you're using. Also, what app are you opening "test.dat" in? I recommend opening it with something that shows you hex data, like HexFiend.app (google it; it's a free download).

Also make sure you're opening the correct test.dat file.

Your posted code worked for me: OS 10.5.6, Xcode 3.1.2.

Output:
Code:
Hello World!

1	0	1	1
chown33 is offline   0 Reply With Quote
Old Sep 5, 2009, 09:49 PM   #3
CharlesRussell3
Thread Starter
macrumors newbie
 
Join Date: Sep 2009
chown33,


Thanks for the response. I'm using Mac OS 10.6, Xcode v. 3.2. Moreover, the app I'm using to open the file is Text Edit.

The source file successfully works, when I code in emacs and execute it from the terminal. Text Edit does read the same values you responded from that method.
The same source file is applied onto XCode, and the build was successful. However, when I open the file via Text Edit, the values are not displayed. It is kinda frustrating to figure out what is problem in this scenario. Thanks.

Charles
CharlesRussell3 is offline   0 Reply With Quote
Old Sep 6, 2009, 12:04 AM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Have you tried to flush fout? Not sure if this will help, but it won't hurt.

-Lee
lee1210 is offline   0 Reply With Quote
Old Sep 6, 2009, 07:53 AM   #5
CharlesRussell3
Thread Starter
macrumors newbie
 
Join Date: Sep 2009
Quote:
Originally Posted by lee1210 View Post
Have you tried to flush fout? Not sure if this will help, but it won't hurt.

-Lee
I have tried that, and it didn't help. I do know the code is not trapping any errors, but it is not spitting any numerical values stated except the usually "Hello World!". Do I have to set permissions of the target directory or somewhere along those lines? If so, how do I proceed with that manner?

Thanks,
Charles
CharlesRussell3 is offline   0 Reply With Quote
Old Sep 6, 2009, 05:05 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by CharlesRussell3 View Post
I have tried that, and it didn't help. I do know the code is not trapping any errors, but it is not spitting any numerical values stated except the usually "Hello World!". Do I have to set permissions of the target directory or somewhere along those lines? If so, how do I proceed with that manner?
First, make sure "test.dat" isn't open in Text Edit. Then trash any existing "test.dat" file, and finally run your program. If a new "test.dat" is created, then permissions isn't the problem.

Change your code so it prints more stuff on the first line, and preferably something that varies. For example, print the current time and date.

Change your code so it prints stuff to stdout, rather than writing to a file. This is to check that it's able to print numeric values.

Use Xcode to open test.dat, rather than TextEdit.

Learn how to set breakpoints in Xcode, then debug your program by setting a breakpoint on main() and stepping through it. Trust me, debugging programs without knowing how to use a debugger is a hellacious task.
chown33 is offline   0 Reply With Quote
Old Dec 30, 2009, 09:46 PM   #7
nofunsir
macrumors member
 
Join Date: Dec 2009
Location: Reno
Temporary Solution

When I upgraded to 10.6 and Xcode 3.2, I got bitten by this bug, too. I could not for the life of me figure out (Still can't!) why a char would write out fine to a (txt) file, but as soon as the program (compiled under 10.6/3.2) tried to write out an integer, not only would it not write the integer, but it would somehow break out of the write-out sequence altogether!

I even created a dummy program that I knew would work (to make sure I wasn't going crazy) and still encountered the same issue. Strangely, the same exact code works as expected in the 10.5/3.1 setup.

Took me forever to find this (temporary) solution: I go to the 'Project' menu and select Edit Project Settings. Then I go to the 'Build' section of the Project Info window that pops up. What works for me is making sure Architectures is on "Standard (32-bit Universal)" and also having Base SDK set to "Mac OS X 10.5". Once that is set, I go to the main project window, and in the upper left hand corner should be a drop-down menu, from which I ensure 10.5 is selected.

I still haven't found what the problem is. And I'm currently trying to find the solution to the problem before I upgrade my iMac's Xcode to 3.2. I think it may be a 64-bit problem, but I wouldn't know.

Is anyone else aware of this issue? Does anyone know if it has been or can be resolved?
nofunsir is offline   0 Reply With Quote
Old Dec 31, 2009, 11:22 AM   #8
Detrius
macrumors 68000
 
Join Date: Sep 2008
Location: Asheville, NC
See if this helps: http://forums.macrumors.com/showpost...53&postcount=4
Detrius is offline   0 Reply With Quote
Old Jan 2, 2010, 11:16 AM   #9
MonkeyCookie
macrumors newbie
 
Join Date: Apr 2008
I also was able to reproduce this problem in XCode 3.2.1, which is currently the latest version of XCode. I'm also running Snow Leopard. It's a big problem if fstream is unable to write to a file.

I found that fstream stopped writing the moment it encountered an integer or double, but no error is ever generated. The program exits normally.

I've also found that, as another poster noted, changing the SDK to 10.5 in Target Info->Build->Base SDK will cause the program to function as expected. It's only the 10.6 SDK that causes the problem.

So there's no need to worry about not upgrading to Snow Leopard or XCode 3.2. It can be resolved with a build setting.

I've just filed a detailed bug report with Apple. We'll see if anything comes of it. I'm sure someone at Apple is aware this by now. fstream not working properly is pretty noticeable: a lot of C++ code uses it.
MonkeyCookie is offline   0 Reply With Quote
Old Jan 2, 2010, 08:39 PM   #10
Detrius
macrumors 68000
 
Join Date: Sep 2008
Location: Asheville, NC
Do I need to repeat the link? Did you remove the predefined macros?
Detrius is offline   0 Reply With Quote
Old Jan 2, 2010, 11:20 PM   #11
twoslick
macrumors newbie
 
Join Date: Jan 2010
This could be a gcc problem. I copied the compile command that Xcode uses, and simply removed the preprocessor directive "-D_GLIBCXX_DEBUG_PEDANTIC=1", and it started working correctly.
twoslick 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
The App Store app thinks Xcode is installed, but I deleted Xcode. moonman239 Mac Applications and Mac App Store 3 May 6, 2014 05:53 PM
Moving from xcode 3.2 to xcode 4.5.2 compilation error for 'abs' macro in stdlib.h hrishidev Mac Programming 7 Jun 2, 2013 10:36 AM
Xcode 4.5 iNeedAnsurs iPhone 0 Jan 25, 2013 02:49 PM
Xcode 3 vs Xcode 4 when dealing with Objective-C timebourne Mac Programming 8 Sep 23, 2012 05:36 PM
XCode 4.4.1 to Xcode 4.5 in Mountain Lion VinegarTasters OS X 10.8 Mountain Lion 2 Sep 19, 2012 08:55 PM

Forum Jump

All times are GMT -5. The time now is 05:09 PM.

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

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