xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/fstream.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_fstream
13 
14 // explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
15 
16 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
17 
18 #include <fstream>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "platform_support.h"
23 #include "operator_hijacker.h"
24 
25 int main(int, char**)
26 {
27     std::string temp = get_temp_file_name();
28     {
29         std::fstream fs(temp,
30                         std::ios_base::in | std::ios_base::out
31                                           | std::ios_base::trunc);
32         double x = 0;
33         fs << 3.25;
34         fs.seekg(0);
35         fs >> x;
36         assert(x == 3.25);
37     }
38     std::remove(temp.c_str());
39 
40     {
41       std::basic_fstream<char, operator_hijacker_char_traits<char> > fs(
42           temp, std::ios_base::in | std::ios_base::out | std::ios_base::trunc);
43       std::basic_string<char, operator_hijacker_char_traits<char> > x;
44       fs << "3.25";
45       fs.seekg(0);
46       fs >> x;
47       assert(x == "3.25");
48     }
49     std::remove(temp.c_str());
50 
51 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
52     {
53         std::wfstream fs(temp,
54                          std::ios_base::in | std::ios_base::out
55                                            | std::ios_base::trunc);
56         double x = 0;
57         fs << 3.25;
58         fs.seekg(0);
59         fs >> x;
60         assert(x == 3.25);
61     }
62     std::remove(temp.c_str());
63 
64     {
65       std::basic_fstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(
66           temp, std::ios_base::in | std::ios_base::out | std::ios_base::trunc);
67       std::basic_string<wchar_t, operator_hijacker_char_traits<wchar_t> > x;
68       fs << L"3.25";
69       fs.seekg(0);
70       fs >> x;
71       assert(x == L"3.25");
72     }
73     std::remove(temp.c_str());
74 #endif
75 
76   return 0;
77 }
78