Jump to content
  • Advertisement
Sign in to follow this  

Save file path

This topic is 4382 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

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. myfile.open ("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
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!