C++ in Xcode

Discussion in 'Mac Programming' started by CharlesRussell3, Sep 5, 2009.

  1. macrumors newbie

    Joined:
    Sep 5, 2009
    #1
    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::eek:ut);

    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
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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
    
     
  3. thread starter macrumors newbie

    Joined:
    Sep 5, 2009
    #3
    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
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Have you tried to flush fout? Not sure if this will help, but it won't hurt.

    -Lee
     
  5. thread starter macrumors newbie

    Joined:
    Sep 5, 2009
    #5
    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
     
  6. macrumors 603

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. macrumors member

    nofunsir

    Joined:
    Dec 30, 2009
    Location:
    Reno
    #7
    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? :apple:
     
  8. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
  9. macrumors newbie

    Joined:
    Apr 18, 2008
    #9
    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.
     
  10. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #10
    Do I need to repeat the link? Did you remove the predefined macros?
     
  11. macrumors newbie

    Joined:
    Jan 2, 2010
    #11
    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.
     

Share This Page