fstream::open can't find file?

Apr 30, 2014 at 9:34pm
Hello. I'm trying to open a file named "global.wdf" for editing. However, is_open() is always returning false. I'm putting global.wdf into my TestProject\Debug folder (which is currently the same directory the .exe file resides in), but is_open() still returns false. Any idea what I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

inline void addl(string str);

int main()
{
	addl("Welcome!");

	fstream f;
	f.open("global.wdf", ios::binary);

	if (f.is_open())
	{
		f.close();
		addl("Successfully opened global.wdf.");
	}
	else
	{
		addl("Unable to open global.wdf.");
	}

	addl("\nPress enter to close the program.");
	getchar();
	return 0;
}

inline void addl(string str)
{
	cout << str << '\n';
}
Apr 30, 2014 at 9:45pm
If you're running the program through the debugger, you should know that the working directory (which affects the behavior of file-related functions when relative paths are given) may not necessarily be the directory the executable is in.
Apr 30, 2014 at 10:08pm
I've tried putting global.wdf in pretty much every folder of the project. Even when running the executable directly (outside of the IDE), the program still can't find the file.
Apr 30, 2014 at 10:12pm
closed account (j3Rz8vqX)
You had it previously in:
...\TestProject\Debug


Try it in the
TestProject
directory.
Apr 30, 2014 at 10:15pm
I tried that as well. is_open() is still returning false.

The IDE I'm using is Microsoft Visual Studio Express 2013 for Windows Desktop, if that means anything.
Apr 30, 2014 at 10:28pm
closed account (j3Rz8vqX)
What do you want.....
f.open("global.wdf", ios::in|ios::out|ios::binary);

Out? In? or both?(as the above applied)

Cannot open without the specific =D

Have a good day; make sure to have the file where the project file is, if running from an ide.
Apr 30, 2014 at 10:35pm
Adding the ios::in and ios::out flags fixed my problem and now the file actually opens! Thank you very much!
Apr 30, 2014 at 10:49pm
closed account (j3Rz8vqX)
Your welcome.
Topic archived. No new replies allowed.