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_ofstream 14 15 // void swap(basic_ofstream& rhs); 16 17 #include <fstream> 18 #include <utility> 19 #include <cassert> 20 #include "platform_support.h" 21 get_temp_file_names()22std::pair<std::string, std::string> get_temp_file_names() { 23 std::pair<std::string, std::string> names; 24 names.first = get_temp_file_name(); 25 26 // Create the file so the next call to `get_temp_file_name()` doesn't 27 // return the same file. 28 std::FILE *fd1 = std::fopen(names.first.c_str(), "w"); 29 30 names.second = get_temp_file_name(); 31 assert(names.first != names.second); 32 33 std::fclose(fd1); 34 std::remove(names.first.c_str()); 35 36 return names; 37 } 38 main()39int main() 40 { 41 std::pair<std::string, std::string> temp_files = get_temp_file_names(); 42 std::string& temp1 = temp_files.first; 43 std::string& temp2 = temp_files.second; 44 assert(temp1 != temp2); 45 { 46 std::ofstream fs1(temp1.c_str()); 47 std::ofstream fs2(temp2.c_str()); 48 fs1 << 3.25; 49 fs2 << 4.5; 50 fs1.swap(fs2); 51 fs1 << ' ' << 3.25; 52 fs2 << ' ' << 4.5; 53 } 54 { 55 std::ifstream fs(temp1.c_str()); 56 double x = 0; 57 fs >> x; 58 assert(x == 3.25); 59 fs >> x; 60 assert(x == 4.5); 61 } 62 std::remove(temp1.c_str()); 63 { 64 std::ifstream fs(temp2.c_str()); 65 double x = 0; 66 fs >> x; 67 assert(x == 4.5); 68 fs >> x; 69 assert(x == 3.25); 70 } 71 std::remove(temp2.c_str()); 72 { 73 std::wofstream fs1(temp1.c_str()); 74 std::wofstream fs2(temp2.c_str()); 75 fs1 << 3.25; 76 fs2 << 4.5; 77 fs1.swap(fs2); 78 fs1 << ' ' << 3.25; 79 fs2 << ' ' << 4.5; 80 } 81 { 82 std::wifstream fs(temp1.c_str()); 83 double x = 0; 84 fs >> x; 85 assert(x == 3.25); 86 fs >> x; 87 assert(x == 4.5); 88 } 89 std::remove(temp1.c_str()); 90 { 91 std::wifstream fs(temp2.c_str()); 92 double x = 0; 93 fs >> x; 94 assert(x == 4.5); 95 fs >> x; 96 assert(x == 3.25); 97 } 98 std::remove(temp2.c_str()); 99 } 100