ifstream loading binary files default path?

Started by
5 comments, last by Dragonsoulj 10 years, 2 months ago

I'm trying to load a binary file, and after testing to see if it passed, it fails every time. I assumed just putting the file name in the quotes would assume the default path of the exe. For example, lets say your exe is stuffed away in some folder such as C:\Programming\Loading Binary Files\ and you have a file called test.bin in the same folder as the exe. It still fails with this simple code:


#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream file ("test.bin", ios::in | ios::binary);
	if(file)
	{
		cout << "passed" << endl;
		file.close();
	}
	else
		cout << "fail" << endl;
	
	system("pause");
	return 0;
}

So without having to type an entire path of the test.bin, what is the default path of the file assuming if you were to just put the file name in quotes and not a full file path? Thanks in advance.

Advertisement
The working directory in default Visual Studio C++ projects is the directory containing the project file. You can change the working directory to be the executable directory by setting it to $(OutDir). Don't forget to also set this for the Release mode configuration as well, if you haven't already selected "All Configurations".

Nvm. I found the file needed to be with the .cpp files, not the debug directory where your exe is located.

[EDIT] Hmmm interesting. I'm changing that now

Well I tried setting the $(OutDir)\ in the Configuration Properties > Output Directory and also have my project in Release mode rather than Debug and it still wont read the file within the path of the exe. Unless I missed something. I dont plan on keeping the file within my source code files forever.

You want Working Directory in the Debugging section of Project Properties.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Whoops wrong place. I did it in General lol. Thanks for the fix.

When you are using VS to run it, the directory won't default to the .exe one, but outside of VS, running stand-alone, it should default to your .exe.

This topic is closed to new replies.

Advertisement