windows 跨平台处理文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/122455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:17:29  来源:igfitidea点击:

Handling file paths cross platform

c++windowslinuxcross-platformgnu

提问by Superpolock

Do any C++ GNU standalone classes exist which handle paths cross platform? My applications build on Windows and LInux. Our configuration files refer to another file in a seperate directory. I'd like to be able to read the path for the other configuration file into a class which would work on both Linux or Windows.

是否存在处理跨平台路径的 C++ GNU 独立类?我的应用程序构建在 Windows 和 Linux 上。我们的配置文件引用了单独目录中的另一个文件。我希望能够将其他配置文件的路径读入一个可以在 Linux 或 Windows 上运行的类。

Which class would offer the smallest footprint to translate paths to use on either system? Thanks

哪个类将提供最小的占用空间来转换在任一系统上使用的路径?谢谢

回答by Adam Rosenfield

Unless you're using absolute paths, there's no need to translate at all - Windows automatically converts forward slashes into backslashes, so if you use relative paths with forward slash path separators, you'll be golden. You should really avoid absolute paths if at all possible.

除非您使用绝对路径,否则根本不需要翻译 - Windows 会自动将正斜杠转换为反斜杠,因此如果您使用带有正斜杠路径分隔符的相对路径,您将获得成功。如果可能的话,您真的应该避免使用绝对路径。

回答by gbjbaanb

回答by Jonas Gulle

Filesystem libraryin boost will probably help you.

boost中的文件系统库可能会帮助你。

回答by user1976

There are many ways, IMHO the correct answer is to redesign your program to avoid manipulating paths. I posted an answer here: https://stackoverflow.com/a/40980510/2345997which is relevant.

有很多方法,恕我直言,正确的答案是重新设计您的程序以避免操纵路径。我在这里发布了一个答案:https: //stackoverflow.com/a/40980510/2345997这是相关的。

ways:

方法:

  1. Add a command line option which allows a user to specify the path in question instead of reading it from a config file.
  2. Add a command line option so that the user can specify a base path. Paths in the config file will be interpreted as located under this base path.
  3. Split your config file into three. One file will have cross platform configuration, another file will have windows only configuration and a final file will have Linux only configuration. Then the user can specify the correct path for both Windows and Linux. On windows your program will read the cross-platform config file and the windows only config file. On Linux it will read the cross-platform file and the Linux only config file.
  4. Add preprocessing to your config file parsing. This will allow you to have one config file where the user can make your program ignore some of the lines in the file depending on which OS the program is running on. Therefore, the user will be able to specify the path to the file twice. Once for Linux, and once for Windows.
  5. Change the design so that the files are always located in the same directory as your executable - then the user only specifies file names in the config file rather than paths to files.
  6. Use a simple function that switches "/" to "\". Then document to the user that they must specify paths as Linux paths and this transformation will be applied for windows.
  7. Create your own path mini-language for this and document it to the user. E.g: "/" - specifies a directory separator, {root} - expands to the root of the filesystem, {cwd} - expands to the current directory, {app} - expands to the path to your application etc... Then the user can specify file paths like: {root}/myfiles/bob.txt on both platforms.
  8. Some paths will work on both platforms. E.g: relative paths like ../my files/bill.txt. Restrict your application to only work with these paths. Document this limitation and how your application handles paths to the user.
  1. 添加一个命令行选项,允许用户指定有问题的路径,而不是从配置文件中读取它。
  2. 添加命令行选项,以便用户可以指定基本路径。配置文件中的路径将被解释为位于此基本路径下。
  3. 将您的配置文件一分为三。一个文件将具有跨平台配置,另一个文件将仅具有 Windows 配置,最终文件将具有仅 Linux 配置。然后用户可以为 Windows 和 Linux 指定正确的路径。在 Windows 上,您的程序将读取跨平台配置文件和仅限 Windows 的配置文件。在 Linux 上,它将读取跨平台文件和仅限 Linux 的配​​置文件。
  4. 将预处理添加到您的配置文件解析中。这将允许您拥有一个配置文件,用户可以在其中让您的程序忽略文件中的某些行,具体取决于程序在哪个操作系统上运行。因此,用户将能够两次指定文件的路径。一次用于 Linux,一次用于 Windows。
  5. 更改设计,使文件始终与可执行文件位于同一目录中 - 然后用户仅在配置文件中指定文件名,而不是文件路径。
  6. 使用将“/”切换为“\”的简单函数。然后向用户证明他们必须将路径指定为 Linux 路径,并且此转换将应用于 Windows。
  7. 为此创建您自己的路径迷你语言并将其记录给用户。例如:“/” - 指定一个目录分隔符,{root} - 扩展到文件系统的根目录,{cwd} - 扩展到当前目录,{app} - 扩展到你的应用程序的路径等等......然后用户可以在两个平台上指定文件路径,如:{root}/myfiles/bob.txt。
  8. 一些路径在两个平台上都适用。例如:相对路径,如 ../my files/bill.txt。限制您的应用程序仅使用这些路径。记录此限制以及您的应用程序如何处理用户路径。