# Opening a file without knowing the exact location?

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how do i open a file that is in the same folder as the application? in c++ Insted of
std::ifstream b_file("C:\\red\\blue\\Release\\blagh.txt.txt");


i just need to open the text folder with out having the user having to have it in a certian directory

##### Share on other sites
well simply opening "foo.txt" will try to open the file from the current working directory, MSVC launches programs with working directory set to the project directory instead of the release/debug folder.

Why would you want to change that behaviour? it's what people expects it todo.

##### Share on other sites
Quote:
 Original post by Solancehow do i open a file that is in the same folder as the application?in c++Insted of*** Source Snippet Removed ***i just need to open the text folder with out having the user having to have it in a certian directory

Actually, this doesn't work. The reason is that ifstream and ofstream start their filepath from the directory from which the executable was launched. So to get the directory you listed, lets say that the executable was launched from "C:\program files\loader\loader.exe"

You want to do this:

std::ifstream b_file("..\\..\\red\\blue\\Release\\blagh.txt");

Each "..\\" takes you back one directory. And don't mess with this, unless you have a good reason why, because as DigitalDelusion said, the user expects it either to open from the directory where the executable resides, or from the last directory they loaded/saved from. If you are loading a file that contains info for your program, and comes with the program, like a configuration file, it will probably be in the same directory or a subdirectory of the directory where your executable resides. For example, if you were loading a configuration file, "config.cfg", from a file "configs" that is a subdirectory of your executable's directory, do this:

std::ifstream b_file("configs\\config.cfg");

Damn source tags. They should resize themselves to the amount of code in them :(

##### Share on other sites
Quote:
 Original post by Solancei just need to open the text folder with out having the user having to have it in a certian directory

If you're talking about having a program that can open some file in some user selected folder then you can either have the "working directory" as a user set'able option somewhere. Much like download managers allow you to set the default download folder. Or if you mean on install that the user can select a location where all files will go.

They all work by having the program apply some directory before all files when opening them. Like: std::ifstream b_file( user_dir + "blargh.txt" );
where user_dir is the directory chosen by the user at some point. you can save that location either in the windows registry or in a simple text file in your app folder.

##### Share on other sites
Quote:
 Original post by silverphyre673Damn source tags. They should resize themselves to the amount of code in them :(
Use [code] [/code] tags instead when there are fewer than about 8 lines of code.

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 49
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632965
• Total Posts
3009546
• ### Who's Online (See full list)

There are no registered users currently online

×