PDA

View Full Version : C++, XCode 3.2 & fstream.h - don't working((




timmsu
Oct 2, 2009, 05:50 PM
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:
#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
Oct 2, 2009, 06:05 PM
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
Oct 2, 2009, 07:00 PM
Could do it like this:

#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
Oct 2, 2009, 07:05 PM
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
Oct 2, 2009, 10:12 PM
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.


#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
Oct 3, 2009, 03:02 AM
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
Oct 3, 2009, 05:36 AM
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
Oct 4, 2009, 01:17 PM
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
Oct 31, 2009, 11:33 AM
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
Nov 9, 2009, 01:24 AM
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:
/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:
GCC_PREPROCESSOR_DEFINITIONS = (
"_GLIBCXX_DEBUG=1",
"_GLIBCXX_DEBUG_PEDANTIC=1",
);

CuteBoA
Mar 13, 2010, 03:45 AM
how do i destroy those things?
because i have the same problem

chown33
Mar 13, 2010, 01:34 PM
how do i destroy those things?
because i have the same problem

http://forums.macrumors.com/showthread.php?t=867839