Home | History | Annotate | Download | only in ofstream.assign
      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 // template <class charT, class traits>
     16 //   void swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
     17 
     18 #include <fstream>
     19 #include <cassert>
     20 #include "platform_support.h"
     21 
     22 int main()
     23 {
     24     std::string temp1 = get_temp_file_name();
     25     std::string temp2 = get_temp_file_name();
     26     {
     27         std::ofstream fs1(temp1.c_str());
     28         std::ofstream fs2(temp2.c_str());
     29         fs1 << 3.25;
     30         fs2 << 4.5;
     31         swap(fs1, fs2);
     32         fs1 << ' ' << 3.25;
     33         fs2 << ' ' << 4.5;
     34     }
     35     {
     36         std::ifstream fs(temp1.c_str());
     37         double x = 0;
     38         fs >> x;
     39         assert(x == 3.25);
     40         fs >> x;
     41         assert(x == 4.5);
     42     }
     43     std::remove(temp1.c_str());
     44     {
     45         std::ifstream fs(temp2.c_str());
     46         double x = 0;
     47         fs >> x;
     48         assert(x == 4.5);
     49         fs >> x;
     50         assert(x == 3.25);
     51     }
     52     std::remove(temp2.c_str());
     53     {
     54         std::wofstream fs1(temp1.c_str());
     55         std::wofstream fs2(temp2.c_str());
     56         fs1 << 3.25;
     57         fs2 << 4.5;
     58         swap(fs1, fs2);
     59         fs1 << ' ' << 3.25;
     60         fs2 << ' ' << 4.5;
     61     }
     62     {
     63         std::wifstream fs(temp1.c_str());
     64         double x = 0;
     65         fs >> x;
     66         assert(x == 3.25);
     67         fs >> x;
     68         assert(x == 4.5);
     69     }
     70     std::remove(temp1.c_str());
     71     {
     72         std::wifstream fs(temp2.c_str());
     73         double x = 0;
     74         fs >> x;
     75         assert(x == 4.5);
     76         fs >> x;
     77         assert(x == 3.25);
     78     }
     79     std::remove(temp2.c_str());
     80 }
     81