-1

I have a problem with my C++ console program. I need some dictionary files for some translations. So I read this Files in the program and gave them a indirect path to the program folder.

String="translation\\PfadzuDatei\\Datei.txt";

In Debugging-Mode this works great, because VS starts the program in the right directory, but when i release it, and it is called from somewhere else like:

Path of Program: c:\Program.exe

And i start it from: another position:

C:\anyPathInConsole\>c:\Program.exe arg1

The program is not able to find the translation files.

Is there any other possibility to set the Path to the files in other ways or do i have to call the program from C:\

The problem with calling the program from the specific folder is, that the program is started by a nodejs "Child-Prozess" exec function and i don`t know the executing Path.

Alexanov
  • 125
  • 18
  • 1
    I would suggest setting up an environment variable with the path to the dictionary. – Qubit Dec 11 '18 at 12:45
  • You might want to get the [path to executable](https://stackoverflow.com/questions/1528298/get-path-of-executable) and append your relative path to that one - this works, of course, only as long as exe and database retain relative locations to each other... – Aconcagua Dec 11 '18 at 12:48
  • 1
    You can specify a ful path instead the relative path. Or pass the path as the program launch argument. – Dmytro Dadyka Dec 11 '18 at 12:48
  • @DmytroDadyka Full path has the disadvantage that you cannot place it at arbitrary location... – Aconcagua Dec 11 '18 at 12:49
  • On windows, there are ways to include files as resources into the exe - might or not be an option for you. Didn't use that feature for a *long* time, though, so you'd need to find the details to yourself... – Aconcagua Dec 11 '18 at 12:51
  • 1
    @Aconcagua It depends of the author purpose . Maybe this file is always in a strictly defined place – Dmytro Dadyka Dec 11 '18 at 12:52
  • What operating system are you using? Or do you need a cross-platform solution? – Dmytro Dadyka Dec 11 '18 at 12:55
  • @DmytroDadyka Quick and dirty, perhaps(?) acceptable if the tool is for personal use only. As soon as giving the tool away, one imposes a file hierarchy on user's system (possibly) not compliant with his/her usual scheme. Can be quite annoying if a programme (or parts of) is/are only installable at one very specific location... – Aconcagua Dec 11 '18 at 13:00

2 Answers2

0

I found out the path where the "Child-Process" from my nodejs-Server executes the program. It's the Project-Folder, not the folder of my js-File. Thank you for your Input. I copied the files to my project folder. Sorry for wasting of your time.

Alexanov
  • 125
  • 18
0

I do not know what operating system the author uses, I will assume that windows. You can get the absolute path to the file by concatenating path to *.exe and relative file path:

std::string getPath()
{
   char buf[256];
   // Get file name
   GetModuleFileNameA(nullptr, &buf[0], sizeof(buf));

   // Extract path from full name
   std::string path = buf;
   const size_t last_slash_idx = path.rfind('\\');
   if (std::string::npos != last_slash_idx)
   {
      path = path.substr(0, last_slash_idx);
   }
   // Add relative path
   path += "\\";
   path += "translation\\PfadzuDatei\\Datei.txt";
   return path;
}

For lixux readlink("/proc/self/exe", buf, sizeof(buf)); can be used instead GetModuleFileNameA

Dmytro Dadyka
  • 2,208
  • 5
  • 18
  • 31