Jump to content
  • Advertisement
Sign in to follow this  
Solance

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by Solance
how 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 this post


Link to post
Share on other sites
Quote:
Original post by Solance
i 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 this post


Link to post
Share on other sites
Quote:
Original post by silverphyre673
Damn 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!