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 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: libcpp-has-no-filesystem-library
11 
12 // Filesystem is supported on Apple platforms starting with macosx10.15.
13 // UNSUPPORTED: with_system_cxx_lib=macosx10.14
14 // UNSUPPORTED: with_system_cxx_lib=macosx10.13
15 // UNSUPPORTED: with_system_cxx_lib=macosx10.12
16 // UNSUPPORTED: with_system_cxx_lib=macosx10.11
17 // UNSUPPORTED: with_system_cxx_lib=macosx10.10
18 // UNSUPPORTED: with_system_cxx_lib=macosx10.9
19 
20 // <fstream>
21 
22 // plate <class charT, class traits = char_traits<charT> >
23 // class basic_ofstream
24 
25 // void open(const filesystem::path& s, ios_base::openmode mode = ios_base::out);
26 
27 #include <fstream>
28 #include <filesystem>
29 #include <cassert>
30 #include "test_macros.h"
31 #include "platform_support.h"
32 
33 namespace fs = std::filesystem;
34 
35 int main(int, char**) {
36   fs::path p = get_temp_file_name();
37   {
38     std::ofstream fs;
39     assert(!fs.is_open());
40     char c = 'a';
41     fs << c;
42     assert(fs.fail());
43     fs.open(p);
44     assert(fs.is_open());
45     fs << c;
46   }
47   {
48     std::ifstream fs(p.c_str());
49     char c = 0;
50     fs >> c;
51     assert(c == 'a');
52   }
53   std::remove(p.string().c_str());
54   {
55     std::wofstream fs;
56     assert(!fs.is_open());
57     wchar_t c = L'a';
58     fs << c;
59     assert(fs.fail());
60     fs.open(p);
61     assert(fs.is_open());
62     fs << c;
63   }
64   {
65     std::wifstream fs(p.c_str());
66     wchar_t c = 0;
67     fs >> c;
68     assert(c == L'a');
69   }
70   std::remove(p.string().c_str());
71 
72   return 0;
73 }
74