Namespaces
Variants

std::basic_ifstream<CharT,Traits>:: swap

From cppreference.net

void swap ( basic_ifstream & other ) ;
(C++11 이후)

스트림의 상태를 other 와 교환합니다.

이것은 basic_istream < CharT, Traits > :: swap ( other ) rdbuf ( ) - > swap ( other. rdbuf ( ) ) 를 호출하여 수행됩니다.

목차

매개변수

other - 상태를 교환할 스트림

반환값

(없음)

예외

구현에서 정의된 예외를 던질 수 있습니다.

예제

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
bool create_stream(std::fstream& fs, const std::string& path)
{
    try
    {
        std::fstream ts{path, ts.trunc | ts.in | ts.out};
        if (ts.is_open())
        {
            ts.swap(fs); // stream objects are not copyable
            return true;
        }
    {
    catch (...)
    {
        std::cout << "Exception!\n";
    }
    return false;
}
void use_stream(std::fstream& fs)
{
    fs.seekg(0);
    std::string data;
    fs >> data;
    std::cout << "data: " << std::quoted(data) << '\n';
}
int main()
{
    std::fstream fs;
    std::string path = "/tmp/test_file.txt";
    if (create_stream(fs, path))
    {
        fs.write(path.c_str(), path.length());
        use_stream(fs);
    }
}

가능한 출력:

data: "/tmp/test_file.txt"

참고 항목

(C++11)
파일 스트림을 이동시킴
(public member function)
(C++11)
두 개의 basic_filebuf 객체를 교환
(public member function of std::basic_filebuf<CharT,Traits> )