Can't get ifstream to work in Xcode

No matter what I try, I cannot use the following code 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 

in the first cout <(inFile.good ()); it prints 1, and the second prints 0. This tells me that the file opens correctly, but inFile does not work as soon as it is read from it. The file has more than enough lines / characters, so I did not try to read it at the end of the file.

File contents:

 8 2 #level 2 XXXXXXXX X......X X..X..XX XX...X X..XX..X XXXX...X X...T..X XXX..XXX #level 1 XXXXXXXX X......X X..X.XXX XX.XX X..XX..X X......X X^....SX XXX.^XXX 
+3
source share
4 answers

This turned out to be a problem with the X-Code. I created a project on a beans network using the same exact code and had no problems. Weird

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

+2
source

This is a known bug. From Xcode 3.2.1 Release Notes :

The gcc 4.2 compiler is not compatible with the standard C ++ library debugging mode by default. C ++ programs compiled with Xcode 3.2 may not work in the Debug configuration. To fix this, install the compiler version 4.0, or edit the Debug configurations Preprocessor macros and delete the Entries:
_GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

+3
source

I reproduced and tested your code and file, and my result was 11, and both levelLW and numLevels were installed as expected. I would definitely carefully consider hidden characters in your file (or their absence). I like to use Notepad ++ with "Show all characters" enabled. My file is exactly what you sent with a carriage return and a translation line at the end of each line.

+1
source

You said the first inFile.good () prints 1. This should mean the file is open OK. Since you said that β€œit works through the terminal, but not Xcode” in the tehMick message - what it costs - when I tested this, I encountered the following problem: my IDE (C ++ Builder) runs a program from the DEBUG directory (when You are in debug mode). I needed to place "sampleplanet" in the DEBUG directory or use a publicly accessible path that found a file like ".. \\ sampleplanet".

 +MyProjectDirectory | mymain.cpp (Even though this is where I had the source file..) | sampleplanet +--DebugDirectory mymain.obj mymain.exe (the program runs out of this directory.) 

As soon as I took care of the above problem, everything worked as expected using the above code and file. I checked the file in Notepadd ++ to confirm [CR] [LF] after each line. If you create the file on Linux, it probably only has [LF] (I did everything under Windows).

+1
source

All Articles