1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <fstream> 11 12 // template <class charT, class traits = char_traits<charT> > 13 // class basic_fstream 14 15 // void swap(basic_fstream& rhs); 16 17 #include <fstream> 18 #include <cassert> 19 #include "platform_support.h" 20 main()21int main() 22 { 23 std::string temp1 = get_temp_file_name(); 24 std::string temp2 = get_temp_file_name(); 25 { 26 std::fstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out 27 | std::ios_base::trunc); 28 std::fstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out 29 | std::ios_base::trunc); 30 fs1 << 1 << ' ' << 2; 31 fs2 << 2 << ' ' << 1; 32 fs1.seekg(0); 33 fs1.swap(fs2); 34 fs1.seekg(0); 35 int i; 36 fs1 >> i; 37 assert(i == 2); 38 fs1 >> i; 39 assert(i == 1); 40 i = 0; 41 fs2 >> i; 42 assert(i == 1); 43 fs2 >> i; 44 assert(i == 2); 45 } 46 std::remove(temp1.c_str()); 47 std::remove(temp2.c_str()); 48 { 49 std::wfstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out 50 | std::ios_base::trunc); 51 std::wfstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out 52 | std::ios_base::trunc); 53 fs1 << 1 << ' ' << 2; 54 fs2 << 2 << ' ' << 1; 55 fs1.seekg(0); 56 fs1.swap(fs2); 57 fs1.seekg(0); 58 int i; 59 fs1 >> i; 60 assert(i == 2); 61 fs1 >> i; 62 assert(i == 1); 63 i = 0; 64 fs2 >> i; 65 assert(i == 1); 66 fs2 >> i; 67 assert(i == 2); 68 } 69 std::remove(temp1.c_str()); 70 std::remove(temp2.c_str()); 71 } 72