xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/ofstream.members/open_path.pass.cpp (revision c352fa7407122ee62d990d6b82551650149f98d4)
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 
11 // UNSUPPORTED: availability-filesystem-missing
12 
13 // <fstream>
14 
15 // plate <class charT, class traits = char_traits<charT> >
16 // class basic_ofstream
17 
18 // void open(const filesystem::path& s, ios_base::openmode mode = ios_base::out);
19 
20 #include <fstream>
21 #include <filesystem>
22 #include <cassert>
23 #include "test_macros.h"
24 #include "platform_support.h"
25 
26 namespace fs = std::filesystem;
27 
main(int,char **)28 int main(int, char**) {
29   fs::path p = get_temp_file_name();
30   {
31     std::ofstream fs;
32     assert(!fs.is_open());
33     char c = 'a';
34     fs << c;
35     assert(fs.fail());
36     fs.open(p);
37     assert(fs.is_open());
38     fs << c;
39   }
40   {
41     std::ifstream fs(p.c_str());
42     char c = 0;
43     fs >> c;
44     assert(c == 'a');
45   }
46   std::remove(p.string().c_str());
47 
48 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
49   {
50     std::wofstream fs;
51     assert(!fs.is_open());
52     wchar_t c = L'a';
53     fs << c;
54     assert(fs.fail());
55     fs.open(p);
56     assert(fs.is_open());
57     fs << c;
58   }
59   {
60     std::wifstream fs(p.c_str());
61     wchar_t c = 0;
62     fs >> c;
63     assert(c == L'a');
64   }
65   std::remove(p.string().c_str());
66 #endif
67 
68   return 0;
69 }
70