C++, XCode 3.2 & fstream.h - don't working((

Discussion in 'Mac Programming' started by timmsu, Oct 2, 2009.

  1. macrumors newbie

    Joined:
    Oct 2, 2009
    #1
    I have Snow Leopard and Xcode 3.2. And I need to write little module for my program, which should read file with integer in first line (number of coordinates) and a lot float numbers in other lines (triangulation coordinates).

    So, I wrote this program:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main () {
    	
    	ifstream ffile("pcp.txt", ifstream::in);
    
    	int nu;
    	
    	cout << ffile.is_open() << endl; //Verifying was file opened or not`
    	
    	ffile>>nu; //Trying to read first number from file
    	cout << nu;
    
    	ffile.close();
        return 0;
    }
    It's just first lines, but it don't work well. File pcp.txt located in binary's directory (/build/debug/) and program says that opens it. But file content some numbers like 123 45.67 32.67 etc. And XCode says that nu == 0. I don't know why! This program works perfectly in my friend's Visual Studio 2008. Please, help me!
     
  2. macrumors 65816

    Joined:
    Jan 6, 2004
    #2
    After you create ffile, add a line:

    if(!ffile) cout << "bad!" << endl;

    That'll tell you if the file is really being open or not.
     
  3. macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #3
    Could do it like this:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main () {
            ifstream inputData;
            float    number;
    
            inputData.open("pcp.txt");
            if (!inputData) {
                    cerr << "Err... ummm... sorry, dude, but couldn't open the blasted #@$()@#$! file" << endl;
                    // EPIC FAIL
                    exit(1);
            }
    
            inputData >> number;
    
            while (!inputData.eof() ) {
                    cout << "The number is: " << number << endl;
                    inputData >> number;
            }
    
            inputData.close();
            return 0;
    }
     
  4. macrumors newbie

    Joined:
    Oct 2, 2009
    #4
    I had almost forgotten how much i dislike c++. that's what happens when you pile crap onto a programming language without a vision.

    die c++ die.

    i <3 c, python, even obj-c
     
  5. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    If you're using Xcode 3.2 be aware that in builds running/debugging under the Debug setting istream may, or may not, fail. My solution is to set the debug target to GCC 4.0 when debugging input streams.

    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
        std::string     str;
        int             n = 6;
        std::ifstream   is("pcp.txt");
        if ( is.is_open())
        {
            is >> str;
            std::cout << str << std::endl;
            
            is >> n;
            std::cout << n << std::endl;
        }
    
        is.close();
    
        return 0;
    }
    
     
  6. thread starter macrumors newbie

    Joined:
    Oct 2, 2009
    #6
    1. I already have verification if file opened or not - it opens ok.

    2. When I add while, it just returns zeros, like:

    The number is: 0
    The number is: 0
    The number is: 0
    The number is: 0
    The number is: 0
    The number is: 0
    The number is: 0
    The number is: 0
    The number is: 0
    etc...

    3. Just tryed to use gcc in Terminal - everything works great, but not in XCode((
     
  7. macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    There's actually quite a bit of vision behind C++. Check out "The Design and Evolution of C++" some time.
     
  8. macrumors newbie

    Joined:
    Oct 18, 2008
    #8
    Under Xcode 3.2 when creating a new project based on stdc++ project template the target build settings for Debug configuration adds preprocessor macros which are incompatible with gcc-4.2:

    • _GLIBCXX_DEBUG=1
    • _GLIBXX_DEBUG_PEDANTIC=1

    Destroy them if you want Debug/gcc-4.2 to execute correctly.
     
  9. macrumors newbie

    Joined:
    Oct 31, 2009
    #9
    Is there a way to make the destruction of these preprocessor macros the default for any new c++ project that is created?
     
  10. macrumors newbie

    Joined:
    Oct 18, 2008
    #10
    Looks like this is still an issue as of Xcode 3.2.1...

    You could try editing the following template project files manually:
    Code:
    /Developer/Library/Xcode/Project Templates/Application/Command Line Tool/C++ Tool/C++Tool.xcodeproj/project.pbxproj
    /Developer/Library/Xcode/Project Templates/Framework & Library/STL C++ Library/C++ Dynamic Library/CppShared.xcodeproj/project.pbxproj
    /Developer/Library/Xcode/Project Templates/Framework & Library/STL C++ Library/C++ Standard Dynamic Library/CppShared.xcodeproj/project.pbxproj
    /Developer/Library/Xcode/Project Templates/System Plug-in/Generic C++ Plug-in/Generic C++ Plugin.xcodeproj/project.pbxproj
    
    And stripping out these (or similar) 4 lines:
    Code:
    GCC_PREPROCESSOR_DEFINITIONS = (
        "_GLIBCXX_DEBUG=1",
        "_GLIBCXX_DEBUG_PEDANTIC=1",
    );
    
     
  11. macrumors newbie

    Joined:
    Mar 12, 2010
    #11
    how do i destroy those things?
    because i have the same problem
     
  12. macrumors 603

    Joined:
    Aug 9, 2009
    #12

Share This Page