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 Oct 2, 2009, 04:50 PM   #1
timmsu
macrumors newbie
 
Join Date: Oct 2009
C++, XCode 3.2 & fstream.h - don't working((

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!
timmsu is offline   0 Reply With Quote
Old Oct 2, 2009, 05:05 PM   #2
darkwing
macrumors 65816
 
Join Date: Jan 2004
Send a message via AIM to darkwing
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.
__________________
A gay man has the same right to marry a woman as I do.
darkwing is offline   0 Reply With Quote
Old Oct 2, 2009, 06:00 PM   #3
electroshock
macrumors 6502a
 
electroshock's Avatar
 
Join Date: Sep 2009
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;
}
electroshock is offline   0 Reply With Quote
Old Oct 2, 2009, 06:05 PM   #4
wakwe
macrumors newbie
 
Join Date: Oct 2009
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
wakwe is offline   0 Reply With Quote
Old Oct 2, 2009, 09:12 PM   #5
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: 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;
}
lloyddean is offline   0 Reply With Quote
Old Oct 3, 2009, 02:02 AM   #6
timmsu
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
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((

Last edited by timmsu; Oct 3, 2009 at 03:42 AM.
timmsu is offline   0 Reply With Quote
Old Oct 3, 2009, 04:36 AM   #7
Sander
macrumors 6502
 
Join Date: Apr 2008
Quote:
Originally Posted by wakwe View Post
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.
Sander is offline   0 Reply With Quote
Old Oct 4, 2009, 12:17 PM   #8
KonaBlend
macrumors newbie
 
Join Date: Oct 2008
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.
KonaBlend is offline   0 Reply With Quote
Old Oct 31, 2009, 10:33 AM   #9
mark517
macrumors newbie
 
Join Date: Oct 2009
Quote:
Originally Posted by KonaBlend View Post
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?
mark517 is offline   0 Reply With Quote
Old Nov 9, 2009, 12:24 AM   #10
KonaBlend
macrumors newbie
 
Join Date: Oct 2008
Quote:
Originally Posted by mark517 View Post
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",
);
KonaBlend is offline   0 Reply With Quote
Old Mar 13, 2010, 02:45 AM   #11
CuteBoA
macrumors newbie
 
Join Date: Mar 2010
how do i destroy those things?
because i have the same problem
CuteBoA is offline   0 Reply With Quote
Old Mar 13, 2010, 12:34 PM   #12
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by CuteBoA View Post
how do i destroy those things?
because i have the same problem
http://forums.macrumors.com/showthread.php?t=867839
chown33 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
Command line tools & Xcode 5 & Mavericks ka-spot OS X Mavericks (10.9) 1 Oct 24, 2013 09:35 AM
Xcode keep creating a provisioning profile that I don't need MacMan988 iPhone/iPad Programming 3 Sep 28, 2013 04:12 AM
xcode error while working help! cesarleitao Mac Programming 1 Dec 7, 2012 07:52 AM
XCode snapshots not working Hanley Mac Programming 0 Jul 16, 2012 10:28 AM
Xcode 4.5 problems - breakpoints I don't want and libc++abi.dylib uncaught exception moonman239 iPhone/iPad Programming 1 Jun 20, 2012 02:09 PM

Forum Jump

All times are GMT -5. The time now is 04:59 AM.

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

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