Sign in to follow this  

Save file path

Recommended Posts

I have written a routine for saving text to a .txt file. The only problem I am having is specifying the path. The following is the line that specifies the name of the file along with where I want it located. ("C:\\temp\\myfile.txt"); This line works correctly. What I want to be able to do is have the file saved to whatever directory the customer chooses to install my application into. In VB you could use "app.path" to do this. Is there an equivalent to ""app.path" in C++? If not, how would I go about doing this? Thanks.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Hi, you probably need to save something in directory, in which your *.exe (application) file is. You should only write relative path. For example, if you need to save "myfile.txt" in application directory, only write "myfile.txt" or ".\\myfile.txt", or in subdirectory "subdirectory\\myfile.txt".

I hope that it helps.
BTW: I'am not sure about my english, :) sorry.

Share this post

Link to post
Share on other sites
That won't quite work. If you run an exe from a different directory on the command line by specifying a full path or by having your exe's path in the PATH variable, the current directory will not be the same as your exe.

Best way is to look at the first parameter passed to your program's main or WinMain function through the command line arguments. This will be the full path to your executable and you can strip off the exe name to get the path.

#include <iostream>
#include <string>

int main(int,const char **av)
std::string s=*av;

std::cout << s << std::endl;

// use string functions to find the LAST occuring '\\'
// and extract the left substring

return 0;

HTH Paul

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this