Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant get ifstream to work in XCode

No matter what I try, I cant get the following code to work correctly.

ifstream inFile;
inFile.open("sampleplanet");
cout << (inFile.good()); //prints a 1
int levelLW = 0;
int numLevels = 0;
inFile >> levelLW >> numLevels;
cout << (inFile.good()); //prints a 0

at the first cout << (inFile.good());, it prints a 1 and at the second a 0. Which tells me that the file is opening correctly, but inFile is failing as soon as read in from it. The file has more then enough lines/characters, so there is no way I have tried to read past the end of the file by that point.

File contents:

8
2
#level 2
XXXXXXXX
X......X
X..X..XX
X.X....X
X..XX..X
XXXX...X
X...T..X
XXX..XXX
#level 1
XXXXXXXX
X......X
X..X.XXX
X.X..X.X
X..XX..X
X......X
X^....SX
XXX.^XXX
like image 983
finiteloop Avatar asked Dec 04 '25 10:12

finiteloop


2 Answers

It's a known bug. From Xcode 3.2.1's Release Notes:

The default gcc 4.2 compiler is not compatible with the Standard C++ Library Debug Mode. C++ programs compiled with Xcode 3.2 may not work in the Debug configuration. To fix this, set the Compiler Version to 4.0, or edit the Debug configuration’s Preprocessor Macros and remove the entries:
_GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

like image 53
JRL Avatar answered Dec 07 '25 01:12

JRL


It turned out to be an issue with X-Code. I created a project in net beans using the same exact code and had no problems. Weird.

Update: In my X-Code project, I changed my active SDK from Mac OS 10.6 to Mac OS 10.5 and everything works fine now.

like image 31
finiteloop Avatar answered Dec 07 '25 00:12

finiteloop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!