0

I'm trying to use fstream to create a file, however the file i'm trying to create wont appear in the .exe directory or anywhere else in the file directory. After searching in my computer for the file, I found that was created in a different directory entirely even though i'm using a relative directory.

This is the code to create the file:

ofstream file;
file.open("something.jpg", ios::out|ios::binary|ios::beg);

Directory of created file: C:\Users\user-pc

Directory of project: D:\Users\user-pc\Documents\Visual Studio 2012\Projects\recvFile

by the way, using an absolute directory works perfectly fine. Could this be a problem with the projects working directory?

Erran
  • 131
  • 1
  • 10
  • 1
    Short answer yes. Set the working directory in project properties. – Dominique McDonnell Aug 18 '15 at 06:32
  • Where do you _think_ the file should be created? As a developer, you should make a conscious decision. "Relative to X" is fine, if you know where X comes from. "Relative to a random directory" is not. – MSalters Aug 18 '15 at 08:47

3 Answers3

1

In your Visual Studio right-click on your project, click Properties, then go to Configuration Properties, then Debugging. There is a row "Working Directory". You can set the working directory there. If you need to do this programmatically, you can use SetCurrentDirectory .

If you need to create the file in the same directory as the .exe location, you can use this approach: https://stackoverflow.com/a/124901/1915854

Call GetModuleFileName() using 0 as a module handle...

If the .exe is installed in a shared directory like Program Files, then creation of the file in the same directory could require additional permissions and may be a bad idea. If the .exe is just cloned to the directory where it should create files, then there is no such problem.

Community
  • 1
  • 1
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • I just noticed that if I run the application from visual sudio, it creates the file int the project directory (which makes sense because the working directory is ProjectDir). But the problem remains; if I run the exe from cmd, the file is created in the wrong directory. – Erran Aug 18 '15 at 06:59
  • You should provide the details in the question on how you run the program from `cmd`. What prompt does `cmd` give? What is the current directory in it? – Serge Rogatch Aug 18 '15 at 07:01
  • apparently I made the mistake of thinking that the exe will create the file in the dir where the exe is stored, not where it is run from. The file is simply created in the current dir of the cmd. – Erran Aug 18 '15 at 07:26
  • @Erran, I've added a description on how to determine the directory of .exe . – Serge Rogatch Aug 18 '15 at 08:15
  • "Creating a file in the directory of the EXE" is a seriously bad idea. Windows 2000 banned it by recommendation, XP started enforcing the rule. With Vista you get further UAC complications. Simply put, the EXE directory is a shared resource and therefore not available for individual user accounts. Now _reading_ files from your EXE directory is perfectly fine and the `GetModuleFileName` method is acceptable for that. – MSalters Aug 18 '15 at 08:44
  • @MSalters, that applies only if he installs the application. If he just copies the `.exe` to the directory where the data should be (kind of portable deployment), then there should be no such problem unless he copies the `.exe` into something like `Program Files`. – Serge Rogatch Aug 18 '15 at 09:01
  • Thanks a bunch guys, you helped me allot. – Erran Aug 18 '15 at 10:41
0

Try adding "../" to the link:

file.open("../something.jpg", ios::out|ios::binary|ios::beg);
Timo Rzipa
  • 41
  • 9
0

File will be created in Debug/Release folder of your project. try what Timo Rzipa suggested.

Raj Samant
  • 41
  • 6