无法让 ifstream 在 XCode 中工作

发布于 2024-08-18 19:03:52 字数 597 浏览 3 评论 0原文

无论我尝试什么,我都无法让以下代码正常工作。

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

在第一次计数 << (inFile.good());,它打印一个 1,第二个打印 0。这告诉我文件打开正确,但是 inFile 在从中读取时就失败了。该文件有足够多的行/字符,因此我无法尝试读取超过文件末尾的内容。

文件内容:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

带上头具痛哭 2024-08-25 19:03:52

这是一个已知的错误。来自 Xcode 3.2.1 的 发行说明

默认的 gcc 4.2 编译器不是
与标准C++兼容
库调试模式。 C++程序
使用 Xcode 3.2 编译可能无法运行
在调试配置中。修复
将编译器版本设置为 4.0,
或编辑调试配置
预处理器宏并删除
条目:
<代码>_GLIBCXX_DEBUG=1
_GLIBCXX_DEBUG_PEDATIC=1

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

愁以何悠 2024-08-25 19:03:52

结果发现是 X-Code 的问题。我使用完全相同的代码在 net beans 中创建了一个项目,没有出现任何问题。诡异的。

更新:
在我的 X-Code 项目中,我将活动 SDK 从 Mac OS 10.6 更改为 Mac OS 10.5,现在一切正常。

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.

软糯酥胸 2024-08-25 19:03:52

我复制并测试了您的代码和文件,我的输出为 11,并且 levelLW 和 numLevels 均按预期设置。我肯定会仔细查看文件中的隐藏字符(或缺少字符)。我喜欢使用启用“显示所有字符”的 Notepad++。我的文件正是您发布的内容,在每行末尾有一个回车符和一个换行符。

I reproduced and tested your code and file and my output was 11 and both levelLW and numLevels were set as expected. I would definitely take a hard look at hidden characters in your file (or lack thereof). I like to use Notepad++ with "Show all characters" enabled. My file is exactly what you posted with a carriage return and a linefeed at the end of each line.

指尖凝香 2024-08-25 19:03:52

你说第一个 inFile.good() 打印出 1 。这应该意味着文件打开正常。因为你在 tehMick 帖子中说“它可以通过终端运行,但不能通过 XCode 运行” - 不管它的价值 - 当我测试这个时,我遇到了以下问题:我的 IDE (C++Builder) 从 DEBUG 目录运行程序(当您处于调试模式时)。我需要将“sampleplanet”放在 DEBUG 目录中,或者使用打开的路径来找到“..\\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.)

一旦我解决了上述问题,使用上述代码和文件一切都按预期工作。我在 Notepadd++ 中检查了文件以确认每行后的 [CR][LF] 。如果您在 Linux 中创建该文件,它可能只有 [LF] (我在 Windows 下完成了所有操作)。

You said the first inFile.good() prints out 1. That should mean the file opened OK. Because you said "it works through the terminal but not XCode" in tehMick post - for what it's worth - when I tested this, I ran into the following problem: my IDE (C++Builder) runs the program out of a DEBUG directory (when you are in debug mode). I needed to place "sampleplanet" in the DEBUG directory or use a path in the open that found the 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.)

Once I took care of the above issue everything worked as expected using the above code and file. I checked the file in Notepadd++ to confirm the [CR][LF] after every line. If you create the file in Linux, it may have only [LF] however (I did everything under Windows).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文