0

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.

DapperDaniel
  • 75
  • 1
  • 10

1 Answers1

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