C/C++有没有使用相对路径的跨平台方式?
我正在用 SDL 编写一个小游戏,文件的结构如下:
“src/game/”同时具有 .h 和 .cpp 源文件。
“data/”包含地图、图块集、精灵等游戏文件...
要加载精灵,例如我将使用以下代码。
spriteLib.loadSprite("data/sprites/sprite-ghost.bmp");
要将这个字符串转换为绝对路径,我在函数的前 4 行中有这些行:
SSprite CSpriteLib::loadSprite(std::string file)
{
//Converting the file path
char converted[128];
realpath(file.c_str(),converted);
file = converted;
但这样程序只能在 liux 下编译,所以...如果有人知道另一种方法,我将不胜感激。
I'm programming a little game in SDL and the files are structured like this:
"src/game/" have both .h and .cpp source files.
"data/" have the game files like maps, tilesets, sprites and so on...
to load a sprite for example I would use the following code.
spriteLib.loadSprite("data/sprites/sprite-ghost.bmp");
to convert this string to an absolute path I have those lines in the first 4 lines of the function:
SSprite CSpriteLib::loadSprite(std::string file)
{
//Converting the file path
char converted[128];
realpath(file.c_str(),converted);
file = converted;
But this way the program only compiles under liux so... if anyone knows another way to do that I would be grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
提升是你的朋友。以下是 Boost 文件系统 教程的链接。
Boost is your friend. Here's a link to the Boost Filesystem tutorial.
几点:
canonicalize_file_name
或char *m=realpath(file.c_str(),0); ... free(m);
- 然而这是 Linux 特定的。请参阅 manrealpath
以了解如何或多或少正确地使用它。另外,realpath 如何帮助您打开数据?如果
适用于您的情况,那么
也可以。提供它是为了帮助删除所有符号链接等。
如果您需要一些与
realpath
类似的功能,您可以在Windows下使用GetFullPathName
,但是它的行为仍然不同。
Few points:
realpath
on some operating systems (Solaris) may still return the relative path.canonicalize_file_name
orchar *m=realpath(file.c_str(),0); ... free(m);
- however this is Linux specific. See manrealpath
to see how to use it more or less correctly.Also how would realpath help you to open your data? If
Works in your case then
would work as well. It is provided to help removing all symbolic links, etc.
If you need some similar functionality to
realpath
, you may useGetFullPathName
under Windows, butit still behaves differently.
刚刚为其编写了一个小类:
将打印:
Just wrote a tiny class for it:
will prints:
我的建议是:首先,您的主程序应该接受本机文件名作为代表程序数据所在位置的参数,例如,
这将决定使用哪些数据的责任传递给其他程序(例如 bash 脚本或驱动程序)或其他)。
其次,使用 unix 格式的数据定义目录空间的子部分,以便程序中的所有文件名文字都具有这种格式。没有如果和但是,始终是 Unix。例如:
现在编写一个函数,将传入的目录名(本机格式)与转换为本机格式的 unix 文件名组合起来,以获得适合访问的文件名。您已经尝试过这样做,但您正在使用依赖于操作系统的功能。不。自己写吧。你想把这个说出来:
@Induscial-antidepressant(以前是@nice金发蠢女孩)的方法也很好,但是需要更多的工作。
My recommendation is: first, your main program should accept a native filename as an argument that represents where the data for the program live eg
This passes the responsibility of deciding which data to use off to some other program (such as a bash script or driver or whatever).
Second, define the subparts of the directory space with the data in unix format so all filename literals in your program have this format. No ifs and buts, always Unix. Eg:
Now write a function which combines the passed in directory name (in native format) with the unix filename translated to native format to get a suitable filename for access. You have already tried to do this but you're using an OS dependent function. Don't. Write it yourself. You want to get this out:
The method of @Industrial-antidepressant (previously @nice blonde stupid girl) is quite good too, but it's a bit more work.