Opening a file without knowing the exact location?

Started by
3 comments, last by iMalc 18 years, 10 months ago
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
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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 :(

my siteGenius is 1% inspiration and 99% perspiration
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.
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement