Jump to content
  • Advertisement
Sign in to follow this  
Coz

How to know where a executable

This topic is 4819 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 noticed that in C++ when you try to open a file without specifing the directory it looks in the working directory. However, if you try to open an executable from another program like this: std::ifstream configurationFile("configuration.data.txt"); the working directory isn't always where the executable is (like some file browsers). How would I find out in which directory my executable is? [Edited by - Coz on September 8, 2005 8:33:50 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well, I just want to open a file. However I want to be able to open that file when the working directory is not the same place where the file is, without having the user to go through the installation. Basicly that the user just downloads a .zip, decompress it and run it.

Share this post


Link to post
Share on other sites
I'm not entirely sure what you're asking, but there's a few helpful functions in the Win32 API (Windows only, obviously): GetModuleFileName will give you the path to the EXE (you can strip off the filename to get the path) and GetCurrentDirectory will give you the current working directory of the program.

You can also specify a relative or an absolute file name to the ifstream constructor, e.g. std::ifstream configurationFile("..\\..\\Data\\configuration.data.txt"); or std::ifstream configurationFile("C:\\Somewhere\\Somehow\\configuration.data.txt");

Share this post


Link to post
Share on other sites
I'm not sure how to be more clear, but it seems that both of you sorta understand what I want. GetModuleFileName does what I want but it's not platform independent. I'll investigate for a platform independant replacement of GetModuleFileName. If there isn't one, I guess I will have to deal with each OS differently XD

If I call program B from program A, and program B thinks that it's working directory is the directory where program A is, when I look for file.txt with a relative path:

Program A is located in directory C:/
Program B is located in directory D:/

Program A starts, using as it's working directory the C:/
Program A calls program B, which also use the C:/ as it's working directory
Program B looks for "file.txt" in it's working directory. However, as file.txt is in D:/file.txt, it is not found.

That's explicitly what I want to solve. In this case, I just have the binaries for program A, but I am coding program B, just in case this helps.

Thank you both for your replies :D

Share this post


Link to post
Share on other sites
Quote:
Original post by Coz
I'm not sure how to be more clear, but it seems that both of you sorta understand what I want. GetModuleFileName does what I want but it's not platform independent. I'll investigate for a platform independant replacement of GetModuleFileName. If there isn't one, I guess I will have to deal with each OS differently XD

I'll save you the trouble: there isn't one.

Share this post


Link to post
Share on other sites
*nix platforms in particular make it very difficult to get the location of a the base executable since its common practice to make links to an executable behave differently depending on the name or even location of the link, so that information gets purposefully discarded. (If you don't understand what that means, that's fine, it's a practice that I personally find moderately insane.)

Share this post


Link to post
Share on other sites
SetCurrentDirectory is useless if I don't know where the files are. I found something that works sometimes, but I'll just have to deal with it differently on each OS.

Still I will need GetModuleFileName(), a command that I wasn't aware of XD

Thanks for all the replies.

Share this post


Link to post
Share on other sites
If A calls program B, it must know it's location. So you can simply change the directory to point to the path of B and change it back after the call.

Bas

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!