std::ifstream::open() 不起作用

发布于 2024-07-10 16:48:10 字数 444 浏览 6 评论 0原文

我正在开发一个游戏原型,某些游戏规则将在ini文件中定义,以便游戏设计者可以调整游戏参数,除了重新编译之外不需要我的帮助。 这就是我目前正在做的事情:

std::ifstream stream;
stream.open("rules.ini");

if (!stream.is_open())
{
    throw new std::exception("Rule file could not be opened");
}

// read file contents here

stream.close();

但是,我的流从未成功打开。 在调试过程中深入研究 STL 源代码会发现 _getstream() (在 Stream.c 中定义)不断返回 NULL,但我就是不明白这是为什么。 帮忙,有人吗?

编辑:Rules.ini 与 .exe 文件位于同一目录中。

I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini file so that the game designers can tweak the game parameters without requiring help from me in addition to a re-compile. This is what I'm doing currently:

std::ifstream stream;
stream.open("rules.ini");

if (!stream.is_open())
{
    throw new std::exception("Rule file could not be opened");
}

// read file contents here

stream.close();

However, my stream never opens succesfully. Diving deep into the STL source during debugging reveals that _getstream() (as defined in stream.c) keeps on returning NULL, but I just can't figure out why this is. Help, anyone?

Edit: Rules.ini is in the same directory as the .exe file.

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

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

发布评论

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

评论(3

嘿哥们儿 2024-07-17 16:48:10

您假设工作目录是可执行文件所在的目录。这是一个错误的假设。

您的可执行文件可以从任何工作目录运行,因此在软件中硬编码相对路径通常不是一个好主意。

如果您希望能够访问与可执行文件位置相关的文件,则应首先确定可执行文件的路径并从中创建一个完全限定的路径。

您可以通过检查传递给 main()argv[0] 参数来获取可执行文件的名称。 或者,如果您使用的是 Windows,则可以通过 获取它GetModuleFileName() 通过传递 NULL 作为第一个参数。

You are assuming that the working directory is the directory that your executable resides in. That is a bad assumption.

Your executable can be run from any working directory, so it's usually a bad idea to hard-code relative paths in your software.

If you want to be able to access files relative to the location of your executable, you should first determine the path of your executable and create a fully qualified path from that.

You can get the name of your executable by examining the argv[0] parameter passed to main(). Alternatively, if you're on Windows, you can get it with GetModuleFileName() by passing NULL as the first parameter.

风吹雨成花 2024-07-17 16:48:10

你的开放流的范围是否正确。

“rules.ini”不是完整路径,因此它必须是相对路径,那么它相对于什么路径。 或者您是否需要在那里使用完整路径。

Is the scope of your open stream correct.

"rules.ini" isn't a full path so it has to be relative so what is it relative to. Or do you need to use full path there.

魂牵梦绕锁你心扉 2024-07-17 16:48:10

(这里疯狂假设)您正在使用 Visual Studio。 在调试期间,您的程序将在项目目录中搜索“rules.ini”,

但是,如果您尝试从“myproject/debug/myexe.exe”执行程序,它应该运行良好,因为它将搜索“/debug” “对于rules.ini

正如所提到的,您应该指定完整路径,因为相对路径往往会导致错误

(wild assumption here) you are using visual studio. During debug, your program is going to search the project directory for "rules.ini"

However, if you try executing your program from "myproject/debug/myexe.exe", it should run fine because it is going to search "/debug" for rules.ini

Like its been mentionned you should specify the full path because relative path tend to lead to errors

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