xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/open_path.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
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++98, c++03, c++11, c++14
10 
11 // <fstream>
12 
13 // basic_filebuf<charT,traits>* open(const filesystem::path& p, ios_base::openmode mode);
14 
15 #include <fstream>
16 #include <filesystem>
17 #include <cassert>
18 #include "platform_support.h"
19 
20 namespace fs = std::filesystem;
21 
22 int main(int, char**) {
23 
24   fs::path p = get_temp_file_name();
25   {
26     std::filebuf f;
27     assert(f.open(p, std::ios_base::out) != 0);
28     assert(f.is_open());
29     assert(f.sputn("123", 3) == 3);
30   }
31   {
32     std::filebuf f;
33     assert(f.open(p, std::ios_base::in) != 0);
34     assert(f.is_open());
35     assert(f.sbumpc() == '1');
36     assert(f.sbumpc() == '2');
37     assert(f.sbumpc() == '3');
38   }
39   std::remove(p.c_str());
40   {
41     std::wfilebuf f;
42     assert(f.open(p, std::ios_base::out) != 0);
43     assert(f.is_open());
44     assert(f.sputn(L"123", 3) == 3);
45   }
46   {
47     std::wfilebuf f;
48     assert(f.open(p, std::ios_base::in) != 0);
49     assert(f.is_open());
50     assert(f.sbumpc() == L'1');
51     assert(f.sbumpc() == L'2');
52     assert(f.sbumpc() == L'3');
53   }
54   remove(p.c_str());
55 
56   return 0;
57 }
58