Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

timmsu

macrumors newbie
Original poster
Oct 2, 2009
2
0
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!
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
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.
 

electroshock

macrumors 6502a
Sep 7, 2009
641
0
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;
}
 

wakwe

macrumors newbie
Oct 2, 2009
4
0
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
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
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;
}
 

timmsu

macrumors newbie
Original poster
Oct 2, 2009
2
0
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((
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
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

There's actually quite a bit of vision behind C++. Check out "The Design and Evolution of C++" some time.
 

KonaBlend

macrumors member
Oct 18, 2008
39
0
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.
 

mark517

macrumors newbie
Oct 31, 2009
1
0
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.

Is there a way to make the destruction of these preprocessor macros the default for any new c++ project that is created?
 

KonaBlend

macrumors member
Oct 18, 2008
39
0
Is there a way to make the destruction of these preprocessor macros the default for any new c++ project that is created?

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",
);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.