std文件流的相对路径
Relative path for std file streams
是否有可能为std::ifstream和std::ofstream提供一个根文件夹,在此之后只有相对路径?
例如:SetFileStreamRootFolder("C:/");
std::ifstream stream("isample.txt"); //C:isample.txt file
std::ofstream stream("osample.txt"); //C:osample.txt file
如果你写一个函数,可以。fstream
-对象不会强加给您任何东西,您可以指定一个相对路径或绝对路径。
Cplusplus.com状态:
Parameters:
filename
String with the name of the file to open.
Specifics about its format and validity
depend on the library implementation and running environment.
您可以定义自己的方法,该方法知道工作目录并在文件名前添加正确的字符串。
std::string prependFilePath(const std::string &filename);
然后用
构造一个流stream(prependFilePath("isample.txt").c_str());
的例子:
std::string prependFilePath(const std::string &filename)
{
// The path can be relative or absolute
return "../" + filename;
}
在实际实现中,您应该将路径(例如:../
)存储在const std::string
成员中,而不是硬编码它,并且可能这种方法是获得静态修饰符(真正的助手/实用程序方法)的良好候选。
当然,使用Boost。文件系统
#include <boost/filesystem.hpp>
...
namespace fs = boost::filesystem;
fs::current_path("C:/");
Filesystem,或类似的东西,预定包含在标准库中。VS2012包含了它的初步实现。所以如果你没有Boost,并且你不想安装它,你可以使用它。
#include <filesystem>
...
namespace fs = std::tr2::sys;
fs::current_path(fs::path("C:/"));
相关文章:
- Windows 和 Linux 之间的相对路径分辨率差异?
- 如何获取 CMake 单元测试的相对路径?
- 从另一个应用启动我的应用时出现相对路径更改问题
- CMake 链接 2 个具有相对路径的库
- cmake find_package CMakeLists.txt 子文件夹中的相对路径
- 在可执行文件中使用带有符号链接的相对路径
- C++流找不到相对路径
- 如何将COM .tlb文件导入具有相对路径C++文件中?
- C++包括断开的相对路径
- CMake 安装具有相对路径的目标
- CMake为什么通过相对路径链接外部库
- Boost Filesystem flowly_caronical不会在没有父文件夹的情况下解决相对路径
- 如何将共享库与具有相对路径的 CMake 链接
- 相对路径中的错误
- C 能够以其完整路径打开代码::块中的文件,但不能以相对路径作为命令行参数打开文件
- 如何在基于QT的Autodesk Maya插件中使用相对路径
- 如何使用QT程序中的默认应用程序打开使用相对路径的PDF
- Visual Studio单元使用具有相对路径的文件测试
- 在 Visual Studio 中为 SFML 设置相对路径
- 如何配置MSVC以使用__FILE__显示头文件的相对路径