Windows 下 fopen 神秘失败

发布于 2024-09-10 01:40:39 字数 779 浏览 2 评论 0原文

也许我只是又黑了一次,但是,这一行给我带来了很多麻烦:

FILE *fp = fopen("data/world.data", "rb");

当用 GCC 编译时,这在 Linux 下工作得很好。但是当我用 Visual Studio 编译它时,它崩溃了。 fp 始终为NULL。 BIN 和 EXE 都位于完全相同的目录中。现在,让事情变得更疯狂的是,当我在 Linux 下使用 Wine 运行 EXE 时……它……有效……

我完全不知道这里发生了什么。也许这对我来说是一些非常愚蠢的错误,但我无法让这个东西在 Windows 下运行:/

另外,我还有另一个程序可以正常工作,那里的数据文件也包含在名为 data 的子目录中。

编辑
明确地说, / NOR `\ * 都不起作用。

编辑2:
好吧,我已经放弃了,也许有人乐于尝试解决这个问题,这里是包含 EXE、VS 调试数据等的 ZIP:
https://dl.dropbox.com/u/2332843/Leaf.zip

编辑3:
使用 CodeBlocks 和 MinGW 编译它,效果非常好。猜测它必须与 MSVC 或 VS 中的项目设置有关。

Maybe I just have another black out but, this one line is giving me a lot of troubles:

FILE *fp = fopen("data/world.data", "rb");

This works fine under Linux when compiled with GCC. But when I compile it with Visual Studio, it crashes. fp is always NULL. Both the BIN and the EXE are in the exact same directory. Now, to make things even crazier, when I run the EXE using Wine under Linux... it... works...

I have absolutely not a god damn clue what's going on here. Maybe it's some insanely stupid mistake on my side, but I cannot get this thing to run under Windows :/

Also, I have another program which works just fine, there the data files are also contained in a sub directory named data.

EDIT:
To make it clear neither / NOR `\ * do work.

EDIT 2:
OK I've given up on this, maybe someone has fun trying to figure it out, here's ZIP containing the EXE, Debug Data for VS etc.:
https://dl.dropbox.com/u/2332843/Leaf.zip

EDIT 3:
Compiled it with CodeBlocks and MinGW, works like a charm. Guess it has to do something with MSVC or the Project Settings in VS.

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

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

发布评论

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

评论(7

來不及說愛妳 2024-09-17 01:40:39

当您运行程序时,听起来 data 不是您的当前目录的子目录。默认情况下(对于 x86 目标)VS 将从您为项目创建的基目录的 DEBUG 或 RELEASE 子目录构建并运行您的程序。您可以修改运行时将成为“当前”的目录(例如,项目 | 属性 | 配置属性 | VS 2008 的调试)。

编辑:虽然 Windows 要求您在命令行中使用反斜杠作为目录分隔符,但正斜杠在代码中工作正常 - 这不是问题的根源。

It sounds like data isn't a subdirectory of your current directory when you run the program. By default (for x86 targets) VS will build and run your program from either a DEBUG or RELEASE subdirectory of the base directory you've created for the project. You can modify the directory that will be "current" when it runs though (e.g., project | properties | configuration properties | debugging for VS 2008).

Edit: While Windows requires that you use a backslash as a directory separator at the command line, a forward slash works fine in code -- this is not the source of your problem.

烟火散人牵绊 2024-09-17 01:40:39

在 Windows 中,您必须编写以下内容:

FILE *fp = fopen("data\\world.data", "rb");

就像这样,因为反斜杠是一个特殊字符(因此字符串中的反斜杠使用 \ 编写,引号符号为 \" ,其他特殊字符也是如此)。

In windows you have to write the following:

FILE *fp = fopen("data\\world.data", "rb");

This is like that because the backslash is a special character (so a backslash in a string is written using \ and a quotation symbol is \" and so with other special characters).

我也只是我 2024-09-17 01:40:39

由于此问题仅发生在 Windows 上。我怀疑该文件是否真的名为“world.data”。如您所知,Windows 的默认设置隐藏文件扩展名。它的真名是world.data.xxx吗?

Since this issue happens only on windows. I doubt whether the file is really named "world.data". As you know, the default setting for windows hides the file extention. Is its real name world.data.xxx?

迷鸟归林 2024-09-17 01:40:39

在 GetCurrentDirectory() 中添加一行,以查看是否从您期望的目录运行。

当我在 Visual Studio 上使用 C#/ C++ 进行开发时,我通常会从调试文件夹中运行它。我认为在 .net 中是否使用正斜杠代替反斜杠并不重要。

Include a line to GetCurrentDirectory(), to see if you are running from the directory you expected.

When I develop in C#/ C++ on visual studio, I normally get to run it from the debug folder. I don't think it matters if forward slash is used in place of backslash in .net.

稍尽春風 2024-09-17 01:40:39

我也遇到了同样的问题,突然想通了。

那应该是你的窗户的错。

比方说,FILE *fp = fopen("data/world.data", "rb"); 在 Windows 中,如果隐藏扩展名,那么您可以看到文件 data/world.data,但实际上它可能是 /data/world.dat.txt 或某种程度。

所以请检查扩展。

希望有帮助!

I happened to have the same problem, and suddenly i figured it out.

That should be your windows fault.

Let's say, FILE *fp = fopen("data/world.data", "rb"); in windows, if you hide the extensions, then you can see the file data/world.data, but actually it maybe /data/world.dat.txt or somewhat.

So please check the extensions.

Hope it helps!

树深时见影 2024-09-17 01:40:39

我今天遇到了这个问题,它发生是因为我在该模式参数上使用了“br”而不是“rb”。

底层 fopen 抛出某种异常,该异常仅记录为崩溃。返回标准 NULL 响应或设置关联的错误值并不麻烦。

I ran into this today, and it happened because I used "br" instead of "rb" on that mode argument.

The underlying fopen is throwing an exception of some kind, which only registers as a crash. It's not bothering to return the standard NULL response or set the associated error values.

度的依靠╰つ 2024-09-17 01:40:39

我不确定,但这可能是因为您在路径中使用斜杠而不是(转义的)反斜杠?

I'm not sure but it may be because you're using slash instead of (an escaped) backslash in the path?

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