Weird question when output results to file.

Discussion in 'Mac Programming' started by pliuya, Feb 20, 2010.

  1. pliuya macrumors newbie

    Feb 20, 2010
    I just started using Xcode and met with a very weird question while outputting results to file. Codes are follows:
    #include <iostream>
    #include <math.h>
    #include <fstream>

    using namespace std;

    int main()

    ofstream trial;"trial.txt");
    trial << "****" << endl;
    trial << (5+3) << endl;
    return 0;

    The outputs in trial.txt are
    I suppose to see 8 but it doesn't appear. However, the results are correct when output to console:

    Does anyone have idea why it happened? Your help will be highly appreciated.

  2. kpua macrumors 6502

    Jul 25, 2006
    You're not closing the stream, so the buffer isn't being flushed and the file contents not written.
  3. pliuya thread starter macrumors newbie

    Feb 20, 2010
    Thank you, but not the case.

    Even I add trial.close(), the problem is still there. I am pretty sure it is due to Xcode. Maybe I miss something in the configuration. There is no problem when I run it using Eclipse.
  4. Detrius macrumors 68000

    Sep 10, 2008
    Asheville, NC
    We get this question every couple days. It's actually an Xcode bug. Edit your target settings and look for this:

    Preprocessor Macros = _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

    Highlight the line, and press the delete key on your keyboard. Now try again.
  5. pliuya thread starter macrumors newbie

    Feb 20, 2010
    Thanks. It works.

    Thank you, Detrius! I follow what you say:
    1. Project -> Edit Active Target ...
    2. Search for "preprocessor" in Build
    3. Delete the value.

    It works correctly!!:):):)

Share This Page