Debian Codegraph @

libcxx/test/std/input.output/file.streams/fstreams/ofstream.cons/pointer.pass.cpp

raw
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 char* s, ios_base::openmode mode = ios_base::out);
15
16 // XFAIL: LIBCXX-AIX-FIXME
17
18 #include <fstream>
19 #include <cassert>
20 #include "test_macros.h"
21 #include "platform_support.h"
22
23 int main(int, char**)
24 {
25 std::string temp = get_temp_file_name();
26 {
27 std::ofstream fs(temp.c_str());
28 fs << 3.25;
29 }
30 {
31 std::ifstream fs(temp.c_str());
32 double x = 0;
33 fs >> x;
34 assert(x == 3.25);
35 }
36 {
37 std::ifstream fs(temp.c_str(), std::ios_base::out);
38 double x = 0;
39 fs >> x;
40 assert(x == 3.25);
41 }
42 std::remove(temp.c_str());
43
44 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
45 {
46 std::wofstream fs(temp.c_str());
47 fs << 3.25;
48 }
49 {
50 std::wifstream fs(temp.c_str());
51 double x = 0;
52 fs >> x;
53 assert(x == 3.25);
54 }
55 {
56 std::wifstream fs(temp.c_str(), std::ios_base::out);
57 double x = 0;
58 fs >> x;
59 assert(x == 3.25);
60 }
61 std::remove(temp.c_str());
62 #endif
63
64 #if TEST_STD_VER >= 23
65 // Test all the noreplace flag combinations
66 {
67 std::ios_base::openmode modes[] = {
68 std::ios_base::out | std::ios_base::noreplace,
69 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
70 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
71 std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary,
72 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary,
73 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace |
74 std::ios_base::binary,
75 };
76 for (auto mode : modes) {
77 std::string tmp = get_temp_file_name(); // also creates the file
78
79 {
80 std::ofstream f(tmp.c_str(), mode);
81 assert(!f.is_open()); // since it already exists
82 }
83
84 {
85 std::remove(tmp.c_str());
86
87 std::ofstream f(tmp.c_str(), mode);
88 assert(f.is_open()); // since it doesn't exist
89 }
90 }
91
92 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
93 for (auto mode : modes) {
94 std::string tmp = get_temp_file_name(); // also creates the file
95
96 {
97 std::wofstream f(tmp.c_str(), mode);
98 assert(!f.is_open()); // since it already exists
99 }
100
101 {
102 std::remove(tmp.c_str());
103
104 std::wofstream f(tmp.c_str(), mode);
105 assert(f.is_open()); // since it doesn't exist
106 }
107 }
108 # endif
109 }
110 #endif // TEST_STD_VER >= 23
111
112 return 0;
113 }