Ivo Wetzel
Ivo Wetzel

Reputation: 46756

fopen fails mysteriously under Windows

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.

Upvotes: 3

Views: 15805

Answers (7)

davenpcj
davenpcj

Reputation: 12704

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.

Upvotes: 0

user1672690
user1672690

Reputation: 23

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!

Upvotes: 0

evol128
evol128

Reputation: 11

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?

Upvotes: 1

Random Guy
Random Guy

Reputation: 21

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

Upvotes: 2

Blessed Geek
Blessed Geek

Reputation: 21684

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.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490808

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.

Upvotes: 6

nico
nico

Reputation: 51690

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

Upvotes: -1

Related Questions