xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp (revision b9a2658a3e8bd13b0f9e7a8a440832a95b377216)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <fstream>
10 
11 // template <class charT, class traits = char_traits<charT> >
12 // class basic_ofstream
13 
14 // explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
15 
16 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
17 
18 #include <fstream>
19 #include <cassert>
20 #include <ios>
21 
22 #include "test_macros.h"
23 #include "operator_hijacker.h"
24 #include "platform_support.h"
25 
26 int main(int, char**)
27 {
28     std::string temp = get_temp_file_name();
29 
30     {
31         std::ofstream fs(temp);
32         fs << 3.25;
33     }
34     {
35         std::ifstream fs(temp);
36         double x = 0;
37         fs >> x;
38         assert(x == 3.25);
39     }
40     std::remove(temp.c_str());
41 
42     {
43       std::basic_ofstream<char, operator_hijacker_char_traits<char> > fs(temp);
44       fs << "3.25";
45     }
46     {
47       std::ifstream fs(temp);
48       double x = 0;
49       fs >> x;
50       assert(x == 3.25);
51     }
52     std::remove(temp.c_str());
53 
54     {
55       std::ofstream fs(temp, std::ios_base::out);
56       fs << 3.25;
57     }
58     {
59       std::ifstream fs(temp);
60       double x = 0;
61       fs >> x;
62       assert(x == 3.25);
63     }
64     std::remove(temp.c_str());
65 
66     {
67       std::basic_ofstream<char, operator_hijacker_char_traits<char> > fs(temp, std::ios_base::out);
68       fs << "3.25";
69     }
70     {
71       std::ifstream fs(temp);
72       double x = 0;
73       fs >> x;
74       assert(x == 3.25);
75     }
76     std::remove(temp.c_str());
77 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
78     {
79         std::wofstream fs(temp);
80         fs << 3.25;
81     }
82     {
83         std::wifstream fs(temp);
84         double x = 0;
85         fs >> x;
86         assert(x == 3.25);
87     }
88     std::remove(temp.c_str());
89 
90     {
91       std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(temp);
92       fs << L"3.25";
93     }
94     {
95       std::wifstream fs(temp);
96       double x = 0;
97       fs >> x;
98       assert(x == 3.25);
99     }
100     std::remove(temp.c_str());
101 
102     {
103       std::wofstream fs(temp, std::ios_base::out);
104       fs << 3.25;
105     }
106     {
107       std::wifstream fs(temp);
108       double x = 0;
109       fs >> x;
110       assert(x == 3.25);
111     }
112     std::remove(temp.c_str());
113 
114     {
115       std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(temp, std::ios_base::out);
116       fs << L"3.25";
117     }
118     {
119       std::wifstream fs(temp);
120       double x = 0;
121       fs >> x;
122       assert(x == 3.25);
123     }
124     std::remove(temp.c_str());
125 #endif
126 
127   return 0;
128 }
129