in my c++ program using the sfml library and xcode, when I load an image I need to load it like picture.loadFromFile("Users/username/Desktop/Xcode/Game/Sprites/MyImage.png")
is there anyway I can shorten that path to just "Game/Sprites/MyImage.png"
. Once again I am using xcode and a mac. Thanks.
Asked
Active
Viewed 410 times
0

DapperDaniel
- 75
- 1
- 10
-
What have you tried? Yes there's a way to get an abbreviated path, although it probably would help to see your efforts first. – l'L'l Feb 26 '18 at 21:10
-
On a Mac you normally put your application's files in the "application bundle". You can read about them in XCode's help. – molbdnilo Feb 26 '18 at 21:18
-
I will look more into that, thanks. – DapperDaniel Feb 26 '18 at 22:03
1 Answers
1
You can use regex:
#include <regex>
std::string full_path = "Users/username/Desktop/Xcode/Game/Sprites/MyImage.png";
std::smatch _match;
std::regex _regex("Game.*");
if(std::regex_search(full_path, _match, _regex))
std::string relative_path = _match.str(); //"Game/Sprites/MyImage.png"

Daniel Marques
- 506
- 5
- 20
-
thanks this did the trick, but if I were to run on this on another computer I would have to change the "Users/username/Desktop/Xcode/etc..." – DapperDaniel Feb 26 '18 at 22:03
-
Based on your question I assumed you always know the full path and you always have a common directory to use in the regex search. – Daniel Marques Feb 26 '18 at 22:12