0

I am coding a C++ file and I want the code to know the path to the exe, that the code will be compiled and linked to. So whatever folder a user moves the exe to, the exe will be able to know the path of its host folder. I understand this is not the same as the current working directory, but I could be wrong!

My objective is for the exe to open files that will reside in the same folder as it regardless of where the user may put the exe.

The code has to be able to append the name of a file, that it wants to open, to this path (with a suitable path delimiter of course) and then open the file. This means that the path returned by whatever function/object does this has to be a std::string or can be read into a std::string.

I have tried:

path = std::experimental::filesystem::current_path(); 
std::string strPath (pthPath);

but the path object returned can't be written to strPath. (FYI: I need to use std::experimental because my IDE (VS2017) does not seem to have std::filesystem). The compiler reports that it can't convert parameter 1 from "std::experimental::filesystem::v1::path" to the string.

I get a similar error if I try

std::string strPath (to_string(pthPath));

Does anyone know how to resolve this or of a better way to meet the same objective?

Chris S
  • 49
  • 4
  • [`pthPath.string()`](https://en.cppreference.com/w/cpp/filesystem/path/string)? – Evg Jun 11 '21 at 07:40
  • It is not possible to reliably find the path in a portable manner. On Windows it should not be needed because the OS will automatically find files residing in the same directory as the executable. – n. m. could be an AI Jun 11 '21 at 07:40
  • Windows uses wide characters for the paths, so you need `std::wstring`. (That this is patform-dependent is very annoying.) – molbdnilo Jun 11 '21 at 07:41
  • "*I understand this is not the same as the current working directory, but I could be wrong!*" - you are not wrong. The filesystem library does not have a way to obtain the exe's path, you have to resort to platform APIs for that. But you can use the filesystem library to parse out the parent folder from that path after you obtain it. – Remy Lebeau Jun 11 '21 at 07:42
  • if you use only file name while file open in code,it will look file from the path where exe present. but if you run the program in VS (debug) then default file location will read from the folder where .vcxproj present – Roshan M Jun 11 '21 at 07:44
  • 1
    Almost impossible to find if you don't know where to look: [GetModuleFileName](https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea). – molbdnilo Jun 11 '21 at 07:45
  • 1
    @n.1.8e9-where's-my-sharem. "*[Windows] will automatically find files residing in the same directory as the executable*" - that is not usually true. Accessing files by relative paths is based on the calling process's current working directory, which is not always the exe folder. The only time Windows looks in the exe folder specifically is when loading DLLs by name without path info. – Remy Lebeau Jun 11 '21 at 07:45
  • 1
    @RoshanM No, it looks in the current working directory. – molbdnilo Jun 11 '21 at 07:46
  • 1
    possible duplicate https://stackoverflow.com/questions/1528298/get-path-of-executable – n. m. could be an AI Jun 11 '21 at 07:49
  • @RemyLebeau right, it will not search, my mistake. – n. m. could be an AI Jun 11 '21 at 08:00
  • Note that, even though it is rare on a desktop OS, a binary executable does not have to exist on disk to be executed. Many embedded systems don’t use filesystems at all, so the concepts of file path is irrelevant for them. This is why any solution to this question will be somewhat platform specific. – op414 Jun 11 '21 at 14:51
  • 1
    Thanks everyone for your suggestions. Given the unreliability of getting the host folder in Windows I went with the simplest solution from @Evg: pthPath.string() and it works for me. – Chris S Jun 15 '21 at 01:49

0 Answers0