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