operator<<,>>(std::filesystem::path)

template< class CharT, class Traits >

std::basic_ostream<CharT,Traits>&

    operator<<( std::basic_ostream<CharT,Traits>& os, const path& p );
(1) (C++17 起)
template< class CharT, class Traits >

std::basic_istream<CharT,Traits>&

    operator>>( std::basic_istream<CharT,Traits>& is, path& p );
(2) (C++17 起)

进行路径 p 上的流输入或输出。使用 std::quoted 以令之后从流输入运算符读取时,空格不导致截断。

目录

参数

os - 要进行输出的流
is - 要进行输入的流
p - 要插入或释出的路径

返回值

1) os
2) is

异常

(无)

可能的实现

版本一
template< class CharT, class Traits >
std::basic_ostream<CharT,Traits>&
    operator<<( std::basic_ostream<CharT,Traits>& os, const path& p )
{
    os << std::quoted(p.string<CharT,Traits>());
    return os;
}
版本二
template< class CharT, class Traits >
std::basic_istream<CharT,Traits>&
    operator>>( std::basic_istream<CharT,Traits>& is, path& p )
{
    std::basic_string<CharT, Traits> t;
    is >> std::quoted(t);
    p = t;
    return is;
}

示例

#include <iostream>
#include <filesystem>
int main()
{
	std::cout << std::filesystem::current_path() << '\n';
}

可能的输出:

"/home/user"

版本历史

  • (当前 | 先前) 2017年5月1日 (一) 07:22Fruderica讨论 | 贡献. . (1,516字节) (+1,516). . (以“{{title|operator<<,>>{{small|(std::filesystem::path)}}}} {{cpp/filesystem/path/navbar}} {{dcl begin}} {{dcl | num=1 | since=c++17 | 1= template< class CharT, class T...”为内容创建页面)