GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::basic_ifstream::swap(3) C++ Standard Libary std::basic_ifstream::swap(3)

std::basic_ifstream::swap - std::basic_ifstream::swap


void swap( basic_ifstream& other ); (since C++11)


Exchanges the state of the stream with those of other.


This is done by calling basic_istream<CharT, Traits>::swap(other) and
rdbuf()->swap(other.rdbuf()).


other - stream to exchange the state with


(none)


May throw implementation-defined exceptions.

// Run this code


#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>


bool create_stream(std::fstream& fs, const std::string& path) {
try {
std::fstream fst { path, std::ios::trunc | std::ios::in | std::ios::out };
if (fst.is_open()) {
fst.swap( fs );
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"


operator= moves the file stream
(C++11) (public member function)
swap swaps two basic_filebuf objects
(C++11) (public member function of std::basic_filebuf<CharT,Traits>)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.