I am sending my C++ program as a zip file. So my code is hardcoded to reference my desktop path. I would like to put these files in a different folder and then send it along with the rest of my code. How can I achieve this? Is there any sort of resource file or something? Any help would be appreciated.
-
3Change absolute paths to relative ones. – AhmadWabbi Aug 05 '18 at 18:00
-
Don't use relative paths at runtime, since you can't guarantee what the process's Current Working Directory is at any given time. Always use absolute paths at runtime. Just don't use hard-coded absolute paths (which the OP is). If you want to use files that are relative in location to your executable, then retrieve the directory path that your executable is running from at runtime and then append subfolders/filenames to that path as needed. – Remy Lebeau Aug 05 '18 at 19:15
-
If you just plan on reading them into your program, you can use initialized variables. It's fairly easy to convert an image into a C++ file containing one const variable which is the image. Generate it in your makefile so the original image file is in the source codebase. – stark Aug 05 '18 at 21:36
-
Related: https://stackoverflow.com/q/1023306/509868 – anatolyg Aug 05 '18 at 21:47
1 Answers
It depends on your platform. There are platform-specific conventions on how to package files with an application.
If you are writing a ANSI/ISO C++ application without use of platform-specific API (like Windows or Mac API calls), those aren't really available, so the best you can do is to include a Readme file that tells your users where the files should go.
At startup, the first argument passed to your command line application would be its path, possibly relative to the current directory. So you should combine those two paths into one to get the path to your programs main executable, then add a relative path relative to your executable's path to look for the additional files.
Alternately, you could just ask your users to pass the path of the folder containing the helper files as an argument to your command line program.

- 8,532
- 36
- 58