【问题标题】:How to shorten the file name/path to an image so it works on all machines如何缩短图像的文件名/路径,使其适用于所有机器
【发布时间】:2018-08-06 09:54:26
【问题描述】:

在我使用 sfml 库和 xcode 的 c++ 程序中,当我加载图像时,我需要像 picture.loadFromFile("Users/username/Desktop/Xcode/Game/Sprites/MyImage.png") 一样加载它,无论如何我可以将该路径缩短为 "Game/Sprites/MyImage.png"。我再次使用 xcode 和 mac。谢谢。

【问题讨论】:

  • 你试过什么?是的,有一种方法可以获得一条简短的路径,尽管它可能有助于首先看到你的努力。
  • 在 Mac 上,您通常将应用程序的文件放在“应用程序包”中。您可以在 XCode 的帮助中了解它们。
  • 我会进一步研究,谢谢。

标签: c++ xcode macos sfml search-path


【解决方案1】:

你可以使用正则表达式:

#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"

【讨论】:

  • 感谢这成功了,但如果我要在另一台计算机上运行它,我将不得不更改“用户/用户名/Desktop/Xcode/etc...”
  • 根据您的问题,我假设您始终知道完整路径,并且您始终有一个通用目录可用于正则表达式搜索。
猜你喜欢
  • 1970-01-01
  • 2012-08-26
  • 2012-10-18
  • 2020-04-26
  • 2016-12-06
  • 2011-12-29
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
相关资源
最近更新 更多