Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c+中从std::ifstream获取文件路径+;_C++_File_Absolute Path - Fatal编程技术网

C++ 如何在c+中从std::ifstream获取文件路径+;

C++ 如何在c+中从std::ifstream获取文件路径+;,c++,file,absolute-path,C++,File,Absolute Path,我使用std::ifstream打开一个文件 我可以使用相对路径(file.txt)或绝对路径(C:\test\file.txt)打开文件 当我传递一个字符串作为文件名时,我不知道它是相对路径还是绝对路径 有谁能告诉我,在文件成功打开后,如何使用 std::ifstream e、 g: 我想在成功打开文件后获取绝对路径 谢谢,< p>流函数类没有访问或处理用于打开文件的名称的功能,C++标准库没有文件名处理功能——您必须自己编写代码,或者使用第三方库或操作系统提供的函数。std::ifstrea

我使用
std::ifstream
打开一个文件

我可以使用相对路径(
file.txt
)或绝对路径(
C:\test\file.txt
)打开文件

当我传递一个字符串作为文件名时,我不知道它是相对路径还是绝对路径

有谁能告诉我,在文件成功打开后,如何使用
std::ifstream

e、 g:

我想在成功打开文件后获取绝对路径


谢谢,

< p>流函数类没有访问或处理用于打开文件的名称的功能,C++标准库没有文件名处理功能——您必须自己编写代码,或者使用第三方库或操作系统提供的函数。
std::ifstream
不存储此信息

但是,您可以做的是:

  • 使用进程的当前工作目录自己组成绝对路径,或
  • 使用类似库的库在相对路径和绝对路径之间进行转换

    boost::filesystem::path abs_path = boost::filesystem::complete("./rel/path");
    std::string abs_path_str = abs_path.string();
    

  • 我认为std::fstream是不可能的。我是在Windows上为一个文件*做的(以一种不可移植的方式)。看


    您是否考虑过使用自己的类来扩展ifstream,该类可以记住文件名?

    您是在使用控制台应用程序,还是在使用框架来实现某种用户界面(UI)?如果投票否决这一5年答案的人留下一条注释来解释错误,这将非常有用。
    boost::filesystem::path abs_path = boost::filesystem::complete("./rel/path");
    std::string abs_path_str = abs_path.string();