Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/ios base/openmode"

From cppreference.com
< cpp‎ | io‎ | ios base
(seealso to filebuf::open which actually shows how these take effect)
(There is a 'See also' section.)
 
(16 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{cpp/io/ios_base/title | openmode}}
+
{{cpp/io/ios_base/title|openmode}}
 
{{cpp/io/ios_base/navbar}}
 
{{cpp/io/ios_base/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list item |  
+
{{dcl|
typedef /*implementation defined*/ openmode;
+
typedef /* implementation defined */ openmode;
 
}}
 
}}
{{ddcl list item | 1=
+
{{dcl|1=
static constexpr openmode app = /*implementation defined*/
+
static constexpr openmode app       = /* implementation defined */;
static constexpr openmode binary = /*implementation defined*/
+
static constexpr openmode binary   = /* implementation defined */;
static constexpr openmode in = /*implementation defined*/
+
static constexpr openmode in       = /* implementation defined */;
static constexpr openmode out = /*implementation defined*/
+
static constexpr openmode out       = /* implementation defined */;
static constexpr openmode trunc = /*implementation defined*/
+
static constexpr openmode trunc     = /* implementation defined */;
static constexpr openmode ate = /*implementation defined*/
+
static constexpr openmode ate       = /* implementation defined */;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl|since=c++23|1=
 +
static constexpr openmode noreplace = /* implementation defined */;
 +
}}
 +
{{dcl end}}
  
Specifies available file open flags. It is a {{concept|BitmaskType}}, the following constants are defined:  
+
Specifies available file open flags. It is a {{named req|BitmaskType}}, the following constants are defined:
 
{{cpp/io/ios_base/openmode consts}}
 
{{cpp/io/ios_base/openmode consts}}
  
 
===Example===
 
===Example===
{{example
+
{{example|lang=cpp|1=
|
+
|
| code=
+
|code=
| output=
+
#include <fstream>
 +
#include <iostream>
 +
#include <string>
 +
 
 +
int main()
 +
{
 +
    const char* fname = "unique_name.txt";
 +
   
 +
    // write to a temporary stream object
 +
    std::fstream(fname, std::ios::out {{!}} std::ios::trunc) << "Hi";
 +
   
 +
    std::string s;
 +
    std::fstream(fname, std::ios::in) >> s;
 +
    std::cout << s << '\n';
 +
}
 +
|output=
 +
Hi
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/io/basic_filebuf/dcl list open}}
+
{{dsc inc|cpp/io/basic_filebuf/dsc open}}
{{dcl list end}}
+
{{dsc inc|cpp/io/basic_stringbuf/dsc constructor}}
 +
{{dsc end}}
  
[[de:cpp/io/ios base/openmode]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/io/ios base/openmode]]
+
[[fr:cpp/io/ios base/openmode]]
+
[[it:cpp/io/ios base/openmode]]
+
[[ja:cpp/io/ios base/openmode]]
+
[[pt:cpp/io/ios base/openmode]]
+
[[ru:cpp/io/ios base/openmode]]
+
[[zh:cpp/io/ios base/openmode]]
+

Latest revision as of 17:43, 20 July 2023

 
 
 
 
typedef /* implementation defined */ openmode;
static constexpr openmode app       = /* implementation defined */;

static constexpr openmode binary    = /* implementation defined */;
static constexpr openmode in        = /* implementation defined */;
static constexpr openmode out       = /* implementation defined */;
static constexpr openmode trunc     = /* implementation defined */;

static constexpr openmode ate       = /* implementation defined */;
static constexpr openmode noreplace = /* implementation defined */;
(since C++23)

Specifies available file open flags. It is a BitmaskType, the following constants are defined:

Constant Explanation
app seek to the end of stream before each write
binary open in binary mode
in open for reading
out open for writing
trunc discard the contents of the stream when opening
ate seek to the end of stream immediately after open
noreplace (C++23) open in exclusive mode

[edit] Example

#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    const char* fname = "unique_name.txt";
 
    // write to a temporary stream object
    std::fstream(fname, std::ios::out | std::ios::trunc) << "Hi";
 
    std::string s;
    std::fstream(fname, std::ios::in) >> s;
    std::cout << s << '\n';
}

Output:

Hi

[edit] See also

opens a file and configures it as the associated character sequence
(public member function of std::basic_filebuf<CharT,Traits>) [edit]
constructs a basic_stringbuf object
(public member function of std::basic_stringbuf<CharT,Traits,Allocator>) [edit]