0

I have several files initialised using paths like this:

  String filePath = "/Users/user1/Documents/UWE/Year_3/SDA/GDA GUI Test/Program_Files/modelgraphic1.png";

They display images and when run on another computer the images do not appear. I recall doing something like this in the past:

  String filePath = "/.../.../.../.../.../.../GDA GUI Test/Program_Files/modelgraphic1.png";

This isn't working. How can I rectify this? Many thanks.

Neo
  • 203
  • 1
  • 11
  • Don't use absolute paths. Use relative paths. Often relative to the executable - look up `-rpath` and `$ORIGIN` as well as "current working directory". – Jesper Juhl Apr 06 '18 at 15:27
  • Possible Duplicate : https://stackoverflow.com/questions/6297738/how-to-build-a-full-path-string-safely-from-separate-strings ? – badola Apr 06 '18 at 15:39

1 Answers1

1

Boost Filesystem is one of the most reliant libraries, when it comes to paths.
Boost Filesystem Docs => https://www.boost.org/doc/libs/1_66_0/libs/filesystem/doc/index.htm

Reasons for using:

  • A modern C++ interface, highly compatible with the C++ standard library.
  • Portability between operating systems.
  • Error handling and reporting via C++ exceptions (the default) or error codes.

Original answer => https://stackoverflow.com/a/6297807/2303176
Sample Code :

#include <iostream>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

int main ()
{
    fs::path dir ("/tmp");
    fs::path file ("foo.txt");
    fs::path full_path = dir / file;
    std::cout << full_path << std::endl;
}

And then running it -

$ g++ ./test.cpp -o test -lboost_filesystem -lboost_system
$ ./test 
/tmp/foo.txt
badola
  • 820
  • 1
  • 13
  • 26