fopen 在 Windows 下神秘失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3276338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:51:53  来源:igfitidea点击:

fopen fails mysteriously under Windows

cwindowsfopen

提问by Ivo Wetzel

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...

当使用 GCC 编译时,这在 Linux 下工作正常。但是当我用 Visual Studio 编译它时,它崩溃了。fp 总是NULL. BIN 和 EXE 都在完全相同的目录中。现在,让事情变得更疯狂,当我在 Linux 下使用 Wine 运行 EXE 时......它......工作......

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 :/

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

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

另外,我还有另一个程序运行良好,数据文件也包含在名为 data.txt 的子目录中。

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

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

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

编辑 2:
好的,我已经放弃了,也许有人想弄清楚它很有趣,这里是包含 EXE、VS 调试数据等的 ZIP:https:
//dl.dropbox.com/u/2332843/Leaf 。压缩

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.

编辑 3:
用 CodeBlocks 和 MinGW 编译它,就像一个魅力。猜测它必须对 MSVC 或 VS 中的项目设置做些什么。

回答by Jerry Coffin

It sounds like dataisn't a subdirectory of your currentdirectory 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).

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

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

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

回答by Random Guy

In windows you have to write the following:

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

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).

之所以这样,是因为反斜杠是一个特殊字符(所以字符串中的反斜杠是用 \ 写的,引号是 \" 等其他特殊字符)。

回答by evol128

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?

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

回答by user1672690

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

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

That should be your windows fault.

那应该是你的windows错误。

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.

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

So please check the extensions.

所以请检查扩展。

Hope it helps!

希望能帮助到你!

回答by davenpcj

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

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

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.

底层的 fopen 正在抛出某种异常,它只注册为崩溃。返回标准 NULL 响应或设置相关的错误值并不麻烦。

回答by Blessed Geek

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

在 GetCurrentDirectory() 中包含一行,以查看您是否从预期的目录运行。

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.

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

回答by nico

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

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