当给定相对路径时从 std::ofstream 变量获取完整系统路径 c++

问题描述 投票:0回答:1

我正在将txt文件写入相对目录路径,这是代码片段

std::ofstream file1("./../../newfile.txt");
file1 << "Something written to file" << endl;
file1.close();

给定的相对路径是

./../../newfile.txt

问题 - 我可以从

file1
变量获取文件在我的计算机中的完整路径吗?我想打印代码中写入文件的最终完整路径。

我确实设置了断点并检查了字段,但它没有显示完整的系统路径。

c++ file-io
1个回答
0
投票

问题是,

fstream
使用路径,他们不存储它。

尽管您可以通过简单地将路径存储在变量中来解决这个缺点。

auto path = "./../../newfile.txt";
std::ostream file1{path};

auto fullPath = std::filesystem::absolute(std::filesystem::path(path));

std::cout << fullPath.c_str();
© www.soinside.com 2019 - 2024. All rights reserved.