From Jason Turner

[fstreams]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpz1vtbq3o/{from.md → to.md} +0 -948
tmp/tmpz1vtbq3o/{from.md → to.md} RENAMED
@@ -1,948 +0,0 @@
1
- ### File streams <a id="fstreams">[[fstreams]]</a>
2
-
3
- The header `<fstream>` defines four class templates and eight types that
4
- associate stream buffers with files and assist reading and writing
5
- files.
6
-
7
- ``` cpp
8
- namespace std {
9
- template <class charT, class traits = char_traits<charT> >
10
- class basic_filebuf;
11
- typedef basic_filebuf<char> filebuf;
12
- typedef basic_filebuf<wchar_t> wfilebuf;
13
-
14
- template <class charT, class traits = char_traits<charT> >
15
- class basic_ifstream;
16
- typedef basic_ifstream<char> ifstream;
17
- typedef basic_ifstream<wchar_t> wifstream;
18
-
19
- template <class charT, class traits = char_traits<charT> >
20
- class basic_ofstream;
21
- typedef basic_ofstream<char> ofstream;
22
- typedef basic_ofstream<wchar_t> wofstream;
23
-
24
- template <class charT, class traits = char_traits<charT> >
25
- class basic_fstream;
26
- typedef basic_fstream<char> fstream;
27
- typedef basic_fstream<wchar_t> wfstream;
28
- }
29
- ```
30
-
31
- In this subclause, the type name `FILE` refers to the type `FILE`
32
- declared in `<cstdio>` ([[c.files]]).
33
-
34
- The class template `basic_filebuf` treats a file as a source or sink of
35
- bytes. In an environment that uses a large character set, the file
36
- typically holds multibyte character sequences and the `basic_filebuf`
37
- object converts those multibyte sequences into wide character sequences.
38
-
39
- #### Class template `basic_filebuf` <a id="filebuf">[[filebuf]]</a>
40
-
41
- ``` cpp
42
- namespace std {
43
- template <class charT, class traits = char_traits<charT> >
44
- class basic_filebuf : public basic_streambuf<charT,traits> {
45
- public:
46
- typedef charT char_type;
47
- typedef typename traits::int_type int_type;
48
- typedef typename traits::pos_type pos_type;
49
- typedef typename traits::off_type off_type;
50
- typedef traits traits_type;
51
-
52
- // [filebuf.cons] Constructors/destructor:
53
- basic_filebuf();
54
- basic_filebuf(const basic_filebuf& rhs) = delete;
55
- basic_filebuf(basic_filebuf&& rhs);
56
- virtual ~basic_filebuf();
57
-
58
- // [filebuf.assign] Assign/swap:
59
- basic_filebuf& operator=(const basic_filebuf& rhs) = delete;
60
- basic_filebuf& operator=(basic_filebuf&& rhs);
61
- void swap(basic_filebuf& rhs);
62
-
63
- // [filebuf.members] Members:
64
- bool is_open() const;
65
- basic_filebuf<charT,traits>* open(const char* s,
66
- ios_base::openmode mode);
67
- basic_filebuf<charT,traits>* open(const string& s,
68
- ios_base::openmode mode);
69
- basic_filebuf<charT,traits>* close();
70
-
71
- protected:
72
- // [filebuf.virtuals] Overridden virtual functions:
73
- virtual streamsize showmanyc();
74
- virtual int_type underflow();
75
- virtual int_type uflow();
76
- virtual int_type pbackfail(int_type c = traits::eof());
77
- virtual int_type overflow (int_type c = traits::eof());
78
-
79
- virtual basic_streambuf<charT,traits>*
80
- setbuf(char_type* s, streamsize n);
81
- virtual pos_type seekoff(off_type off, ios_base::seekdir way,
82
- ios_base::openmode which = ios_base::in | ios_base::out);
83
- virtual pos_type seekpos(pos_type sp,
84
- ios_base::openmode which = ios_base::in | ios_base::out);
85
- virtual int sync();
86
- virtual void imbue(const locale& loc);
87
- };
88
-
89
- template <class charT, class traits>
90
- void swap(basic_filebuf<charT, traits>& x,
91
- basic_filebuf<charT, traits>& y);
92
- }
93
- ```
94
-
95
- The class `basic_filebuf<charT,traits>` associates both the input
96
- sequence and the output sequence with a file.
97
-
98
- The restrictions on reading and writing a sequence controlled by an
99
- object of class `basic_filebuf<charT, traits>` are the same as for
100
- reading and writing with the Standard C library `FILE`s.
101
-
102
- In particular:
103
-
104
- - If the file is not open for reading the input sequence cannot be read.
105
- - If the file is not open for writing the output sequence cannot be
106
- written.
107
- - A joint file position is maintained for both the input sequence and
108
- the output sequence.
109
-
110
- An instance of `basic_filebuf` behaves as described in  [[filebuf]]
111
- provided `traits::pos_type` is `fpos<traits::state_type>`. Otherwise the
112
- behavior is undefined.
113
-
114
- In order to support file I/O and multibyte/wide character conversion,
115
- conversions are performed using members of a facet, referred to as
116
- `a_codecvt` in following sections, obtained as if by
117
-
118
- ``` cpp
119
- const codecvt<charT,char,typename traits::state_type>& a_codecvt =
120
- use_facet<codecvt<charT,char,typename traits::state_type> >(getloc());
121
- ```
122
-
123
- #### `basic_filebuf` constructors <a id="filebuf.cons">[[filebuf.cons]]</a>
124
-
125
- ``` cpp
126
- basic_filebuf();
127
- ```
128
-
129
- *Effects:* Constructs an object of class `basic_filebuf<charT,traits>`,
130
- initializing the base class with
131
- `basic_streambuf<charT,traits>()` ([[streambuf.cons]]).
132
-
133
- `is_open() == false`.
134
-
135
- ``` cpp
136
- basic_filebuf(basic_filebuf&& rhs);
137
- ```
138
-
139
- *Effects:* Move constructs from the rvalue `rhs`. It is
140
- *implementation-defined* whether the sequence pointers in `*this`
141
- (`eback()`, `gptr()`, `egptr()`, `pbase()`, `pptr()`, `epptr()`) obtain
142
- the values which `rhs` had. Whether they do or not, `*this` and `rhs`
143
- reference separate buffers (if any at all) after the construction.
144
- Additionally `*this` references the file which `rhs` did before the
145
- construction, and `rhs` references no file after the construction. The
146
- openmode, locale and any other state of `rhs` is also copied.
147
-
148
- *Postconditions:* Let `rhs_p` refer to the state of `rhs` just prior to
149
- this construction and let `rhs_a` refer to the state of `rhs` just after
150
- this construction.
151
-
152
- - `is_open() == rhs_p.is_open()`
153
- - `rhs_a.is_open() == false`
154
- - `gptr() - eback() == rhs_p.gptr() - rhs_p.eback()`
155
- - `egptr() - eback() == rhs_p.egptr() - rhs_p.eback()`
156
- - `pptr() - pbase() == rhs_p.pptr() - rhs_p.pbase()`
157
- - `epptr() - pbase() == rhs_p.epptr() - rhs_p.pbase()`
158
- - `if (eback()) eback() != rhs_a.eback()`
159
- - `if (gptr()) gptr() != rhs_a.gptr()`
160
- - `if (egptr()) egptr() != rhs_a.egptr()`
161
- - `if (pbase()) pbase() != rhs_a.pbase()`
162
- - `if (pptr()) pptr() != rhs_a.pptr()`
163
- - `if (epptr()) epptr() != rhs_a.epptr()`
164
-
165
- ``` cpp
166
- virtual ~basic_filebuf();
167
- ```
168
-
169
- *Effects:* Destroys an object of class `basic_filebuf<charT,traits>`.
170
- Calls `close()`. If an exception occurs during the destruction of the
171
- object, including the call to `close()`, the exception is caught but not
172
- rethrown (see  [[res.on.exception.handling]]).
173
-
174
- #### Assign and swap <a id="filebuf.assign">[[filebuf.assign]]</a>
175
-
176
- ``` cpp
177
- basic_filebuf& operator=(basic_filebuf&& rhs);
178
- ```
179
-
180
- *Effects:* Calls `this->close()` then move assigns from `rhs`. After the
181
- move assignment `*this` has the observable state it would have had if it
182
- had been move constructed from `rhs` (see  [[filebuf.cons]]).
183
-
184
- *Returns:* `*this`.
185
-
186
- ``` cpp
187
- void swap(basic_filebuf& rhs);
188
- ```
189
-
190
- *Effects:* Exchanges the state of `*this` and `rhs`.
191
-
192
- ``` cpp
193
- template <class charT, class traits>
194
- void swap(basic_filebuf<charT, traits>& x,
195
- basic_filebuf<charT, traits>& y);
196
- ```
197
-
198
- *Effects:* `x.swap(y)`.
199
-
200
- #### Member functions <a id="filebuf.members">[[filebuf.members]]</a>
201
-
202
- ``` cpp
203
- bool is_open() const;
204
- ```
205
-
206
- *Returns:* `true` if a previous call to `open` succeeded (returned a
207
- non-null value) and there has been no intervening call to close.
208
-
209
- ``` cpp
210
- basic_filebuf<charT,traits>* open(const char* s,
211
- ios_base::openmode mode);
212
- ```
213
-
214
- *Effects:* If `is_open() != false`, returns a null pointer. Otherwise,
215
- initializes the `filebuf` as required. It then opens a file, if
216
- possible, whose name is the NTBS`s` (as if by calling
217
- `std::fopen(s,modstr)`). The NTBS`modstr` is determined from
218
- `mode & ~ios_base::ate` as indicated in
219
- Table  [[tab:iostreams.file.open.modes]]. If `mode` is not some
220
- combination of flags shown in the table then the open fails.
221
-
222
- **Table: File open modes** <a id="tab:iostreams.file.open.modes">[tab:iostreams.file.open.modes]</a>
223
-
224
- | `binary` | `in` | `out` | `trunc` | `app` | | `stdio` equivalent |
225
- | -------- | ---- | ----- | ------- | ----- | --- | ------------------ |
226
- | | | + | | | `"w"` |
227
- | | | + | | + | `"a"` |
228
- | | | | | + | `"a"` |
229
- | | | + | + | | `"w"` |
230
- | | + | | | | `"r"` |
231
- | | + | + | | | `"r+"` |
232
- | | + | + | + | | `"w+"` |
233
- | | + | + | | + | `"a+"` |
234
- | | + | | | + | `"a+"` + | | + | | | `"wb"` |
235
- | + | | + | | + | `"ab"` |
236
- | + | | | | + | `"ab"` |
237
- | + | | + | + | | `"wb"` |
238
- | + | + | | | | `"rb"` |
239
- | + | + | + | | | `"r+b"` |
240
- | + | + | + | + | | `"w+b"` |
241
- | + | + | + | | + | `"a+b"` |
242
- | + | + | | | + | `"a+b"` |
243
-
244
-
245
- If the open operation succeeds and `(mode & ios_base::ate) != 0`,
246
- positions the file to the end (as if by calling
247
- `std::fseek(file,0,SEEK_END)`).[^39]
248
-
249
- If the repositioning operation fails, calls `close()` and returns a null
250
- pointer to indicate failure.
251
-
252
- *Returns:* `this` if successful, a null pointer otherwise.
253
-
254
- ``` cpp
255
- basic_filebuf<charT,traits>* open(const string& s,
256
- ios_base::openmode mode);
257
- ```
258
-
259
- *Returns:* `open(s.c_str(), mode);`
260
-
261
- ``` cpp
262
- basic_filebuf<charT,traits>* close();
263
- ```
264
-
265
- *Effects:* If `is_open() == false`, returns a null pointer. If a put
266
- area exists, calls `overflow(traits::eof())` to flush characters. If the
267
- last virtual member function called on `*this` (between `underflow`,
268
- `overflow`, `seekoff`, and `seekpos`) was `overflow` then calls
269
- `a_codecvt.unshift` (possibly several times) to determine a termination
270
- sequence, inserts those characters and calls `overflow(traits::eof())`
271
- again. Finally, regardless of whether any of the preceding calls fails
272
- or throws an exception, the function closes the file (as if by calling
273
- `std::fclose(file)`).[^40] If any of the calls made by the function,
274
- including `std::fclose`, fails, `close` fails by returning a null
275
- pointer. If one of these calls throws an exception, the exception is
276
- caught and rethrown after closing the file.
277
-
278
- *Returns:* `this` on success, a null pointer otherwise.
279
-
280
- `is_open() == false`.
281
-
282
- #### Overridden virtual functions <a id="filebuf.virtuals">[[filebuf.virtuals]]</a>
283
-
284
- ``` cpp
285
- streamsize showmanyc();
286
- ```
287
-
288
- *Effects:* Behaves the same as
289
- `basic_streambuf::showmanyc()` ([[streambuf.virtuals]]).
290
-
291
- *Remarks:* An implementation might well provide an overriding definition
292
- for this function signature if it can determine that more characters can
293
- be read from the input sequence.
294
-
295
- ``` cpp
296
- int_type underflow();
297
- ```
298
-
299
- *Effects:* Behaves according to the description of
300
- `basic_streambuf<charT,traits>::underflow()`, with the specialization
301
- that a sequence of characters is read from the input sequence as if by
302
- reading from the associated file into an internal buffer ( `extern_buf`)
303
- and then as if by doing
304
-
305
- ``` cpp
306
- char extern_buf[XSIZE];
307
- char* extern_end;
308
- charT intern_buf[ISIZE];
309
- charT* intern_end;
310
- codecvt_base::result r =
311
- a_codecvt.in(state, extern_buf, extern_buf+XSIZE, extern_end,
312
- intern_buf, intern_buf+ISIZE, intern_end);
313
- ```
314
-
315
- This shall be done in such a way that the class can recover the position
316
- (`fpos_t`) corresponding to each character between `intern_buf` and
317
- `intern_end`. If the value of `r` indicates that `a_codecvt.in()` ran
318
- out of space in `intern_buf`, retry with a larger `intern_buf`.
319
-
320
- ``` cpp
321
- int_type uflow();
322
- ```
323
-
324
- *Effects:* Behaves according to the description of
325
- `basic_streambuf<charT,traits>::uflow()`, with the specialization that a
326
- sequence of characters is read from the input with the same method as
327
- used by `underflow`.
328
-
329
- ``` cpp
330
- int_type pbackfail(int_type c = traits::eof());
331
- ```
332
-
333
- *Effects:* Puts back the character designated by `c` to the input
334
- sequence, if possible, in one of three ways:
335
-
336
- - If `traits::eq_int_type(c,traits::eof())` returns `false` and if the
337
- function makes a putback position available and if
338
- `traits::eq(to_char_type(c),gptr()[-1])` returns `true`, decrements
339
- the next pointer for the input sequence, `gptr()`. Returns: `c`.
340
- - If `traits::eq_int_type(c,traits::eof())` returns `false` and if the
341
- function makes a putback position available and if the function is
342
- permitted to assign to the putback position, decrements the next
343
- pointer for the input sequence, and stores `c` there. Returns: `c`.
344
- - If `traits::eq_int_type(c,traits::eof())` returns `true`, and if
345
- either the input sequence has a putback position available or the
346
- function makes a putback position available, decrements the next
347
- pointer for the input sequence, `gptr()`. Returns:
348
- `traits::not_eof(c)`.
349
-
350
- *Returns:* `traits::eof()` to indicate failure.
351
-
352
- *Remarks:* If `is_open() == false`, the function always fails.
353
-
354
- The function does not put back a character directly to the input
355
- sequence.
356
-
357
- If the function can succeed in more than one of these ways, it is
358
- unspecified which way is chosen. The function can alter the number of
359
- putback positions available as a result of any call.
360
-
361
- ``` cpp
362
- int_type overflow(int_type c = traits::eof());
363
- ```
364
-
365
- *Effects:* Behaves according to the description of
366
- `basic_streambuf<charT,traits>::overflow(c)`, except that the behavior
367
- of “consuming characters” is performed by first converting as if by:
368
-
369
- ``` cpp
370
- charT* b = pbase();
371
- charT* p = pptr();
372
- charT* end;
373
- char xbuf[XSIZE];
374
- char* xbuf_end;
375
- codecvt_base::result r =
376
- a_codecvt.out(state, b, p, end, xbuf, xbuf+XSIZE, xbuf_end);
377
- ```
378
-
379
- and then
380
-
381
- - If `r == codecvt_base::error` then fail.
382
- - If `r == codecvt_base::noconv` then output characters from `b` up to
383
- (and not including) `p`.
384
- - If `r == codecvt_base::partial` then output to the file characters
385
- from `xbuf` up to `xbuf_end`, and repeat using characters from `end`
386
- to `p`. If output fails, fail (without repeating).
387
- - Otherwise output from `xbuf` to `xbuf_end`, and fail if output fails.
388
- At this point if `b != p` and `b == end` (`xbuf` isn’t large enough)
389
- then increase `XSIZE` and repeat from the beginning.
390
-
391
- *Returns:* `traits::not_eof(c)` to indicate success, and `traits::eof()`
392
- to indicate failure. If `is_open() == false`, the function always fails.
393
-
394
- ``` cpp
395
- basic_streambuf* setbuf(char_type* s, streamsize n);
396
- ```
397
-
398
- *Effects:* If `setbuf(0,0)` is called on a stream before any I/O has
399
- occurred on that stream, the stream becomes unbuffered. Otherwise the
400
- results are *implementation-defined*. “Unbuffered” means that `pbase()`
401
- and `pptr()` always return null and output to the file should appear as
402
- soon as possible.
403
-
404
- ``` cpp
405
- pos_type seekoff(off_type off, ios_base::seekdir way,
406
- ios_base::openmode which = ios_base::in | ios_base::out);
407
- ```
408
-
409
- *Effects:* Let `width` denote `a_codecvt.encoding()`. If
410
- `is_open() == false`, or `off != 0 && width <= 0`, then the positioning
411
- operation fails. Otherwise, if `way != basic_ios::cur` or `off != 0`,
412
- and if the last operation was output, then update the output sequence
413
- and write any unshift sequence. Next, seek to the new position: if
414
- `width > 0`, call `std::fseek(file, width * off, whence)`, otherwise
415
- call `std::fseek(file, 0, whence)`.
416
-
417
- *Remarks:* “The last operation was output” means either the last virtual
418
- operation was overflow or the put buffer is non-empty. “Write any
419
- unshift sequence” means, if `width` if less than zero then call
420
- `a_codecvt.unshift(state, xbuf, xbuf+XSIZE, xbuf_end)` and output the
421
- resulting unshift sequence. The function determines one of three values
422
- for the argument `whence`, of type `int`, as indicated in
423
- Table  [[tab:iostreams.seekoff.effects]].
424
-
425
- **Table: `seekoff` effects** <a id="tab:iostreams.seekoff.effects">[tab:iostreams.seekoff.effects]</a>
426
-
427
- | `way` Value | `stdio` Equivalent |
428
- | ---------------- | ------------------ |
429
- | `basic_ios::beg` | `SEEK_SET` |
430
- | `basic_ios::cur` | `SEEK_CUR` |
431
- | `basic_ios::end` | `SEEK_END` |
432
-
433
-
434
- *Returns:* A newly constructed `pos_type` object that stores the
435
- resultant stream position, if possible. If the positioning operation
436
- fails, or if the object cannot represent the resultant stream position,
437
- returns `pos_type(off_type(-1))`.
438
-
439
- ``` cpp
440
- pos_type seekpos(pos_type sp,
441
- ios_base::openmode which = ios_base::in | ios_base::out);
442
- ```
443
-
444
- Alters the file position, if possible, to correspond to the position
445
- stored in `sp` (as described below). Altering the file position performs
446
- as follows:
447
-
448
- 1. if `(om & ios_base::out) != 0`, then update the output sequence and
449
- write any unshift sequence;
450
- 2. set the file position to `sp`;
451
- 3. if `(om & ios_base::in) != 0`, then update the input sequence;
452
-
453
- where `om` is the open mode passed to the last call to `open()`. The
454
- operation fails if `is_open()` returns false.
455
-
456
- If `sp` is an invalid stream position, or if the function positions
457
- neither sequence, the positioning operation fails. If `sp` has not been
458
- obtained by a previous successful call to one of the positioning
459
- functions (`seekoff` or `seekpos`) on the same file the effects are
460
- undefined.
461
-
462
- *Returns:* `sp` on success. Otherwise returns `pos_type(off_type(-1))`.
463
-
464
- ``` cpp
465
- int sync();
466
- ```
467
-
468
- *Effects:* If a put area exists, calls `filebuf::overflow` to write the
469
- characters to the file. If a get area exists, the effect is
470
- *implementation-defined*.
471
-
472
- ``` cpp
473
- void imbue(const locale& loc);
474
- ```
475
-
476
- If the file is not positioned at its beginning and the encoding of the
477
- current locale as determined by `a_codecvt.encoding()` is
478
- state-dependent ([[locale.codecvt.virtuals]]) then that facet is the
479
- same as the corresponding facet of `loc`.
480
-
481
- *Effects:* Causes characters inserted or extracted after this call to be
482
- converted according to `loc` until another call of `imbue`.
483
-
484
- This may require reconversion of previously converted characters. This
485
- in turn may require the implementation to be able to reconstruct the
486
- original contents of the file.
487
-
488
- #### Class template `basic_ifstream` <a id="ifstream">[[ifstream]]</a>
489
-
490
- ``` cpp
491
- namespace std {
492
- template <class charT, class traits = char_traits<charT> >
493
- class basic_ifstream : public basic_istream<charT,traits> {
494
- public:
495
- typedef charT char_type;
496
- typedef typename traits::int_type int_type;
497
- typedef typename traits::pos_type pos_type;
498
- typedef typename traits::off_type off_type;
499
- typedef traits traits_type;
500
-
501
- // [ifstream.cons] Constructors:
502
- basic_ifstream();
503
- explicit basic_ifstream(const char* s,
504
- ios_base::openmode mode = ios_base::in);
505
- explicit basic_ifstream(const string& s,
506
- ios_base::openmode mode = ios_base::in);
507
- basic_ifstream(const basic_ifstream& rhs) = delete;
508
- basic_ifstream(basic_ifstream&& rhs);
509
-
510
- // [ifstream.assign] Assign/swap:
511
- basic_ifstream& operator=(const basic_ifstream& rhs) = delete;
512
- basic_ifstream& operator=(basic_ifstream&& rhs);
513
- void swap(basic_ifstream& rhs);
514
-
515
- // [ifstream.members] Members:
516
- basic_filebuf<charT,traits>* rdbuf() const;
517
-
518
- bool is_open() const;
519
- void open(const char* s, ios_base::openmode mode = ios_base::in);
520
- void open(const string& s, ios_base::openmode mode = ios_base::in);
521
- void close();
522
- private:
523
- basic_filebuf<charT,traits> sb; // exposition only
524
- };
525
-
526
- template <class charT, class traits>
527
- void swap(basic_ifstream<charT, traits>& x,
528
- basic_ifstream<charT, traits>& y);
529
- }
530
- ```
531
-
532
- The class `basic_ifstream<charT, traits>` supports reading from named
533
- files. It uses a `basic_filebuf<{}charT, traits>` object to control the
534
- associated sequence. For the sake of exposition, the maintained data is
535
- presented here as:
536
-
537
- - `sb`, the `filebuf` object.
538
-
539
- #### `basic_ifstream` constructors <a id="ifstream.cons">[[ifstream.cons]]</a>
540
-
541
- ``` cpp
542
- basic_ifstream();
543
- ```
544
-
545
- *Effects:* Constructs an object of class `basic_ifstream<charT,traits>`,
546
- initializing the base class with `basic_istream(&sb)` and initializing
547
- `sb` with `basic_filebuf<charT,traits>())` ([[istream.cons]],
548
- [[filebuf.cons]]).
549
-
550
- ``` cpp
551
- explicit basic_ifstream(const char* s,
552
- ios_base::openmode mode = ios_base::in);
553
- ```
554
-
555
- *Effects:* Constructs an object of class `basic_ifstream`, initializing
556
- the base class with `basic_istream(&sb)` and initializing `sb` with
557
- `basic_filebuf<charT, traits>())` ([[istream.cons]], [[filebuf.cons]]),
558
- then calls `rdbuf()->open(s, mode | ios_base::in)`. If that function
559
- returns a null pointer, calls `setstate(failbit)`.
560
-
561
- ``` cpp
562
- explicit basic_ifstream(const string& s,
563
- ios_base::openmode mode = ios_base::in);
564
- ```
565
-
566
- *Effects:* the same as `basic_ifstream(s.c_str(), mode)`.
567
-
568
- ``` cpp
569
- basic_ifstream(basic_ifstream&& rhs);
570
- ```
571
-
572
- *Effects:* Move constructs from the rvalue `rhs`. This is accomplished
573
- by move constructing the base class, and the contained `basic_filebuf`.
574
- Next `basic_istream<charT,traits>::set_rdbuf(&sb)` is called to install
575
- the contained `basic_filebuf`.
576
-
577
- #### Assign and swap <a id="ifstream.assign">[[ifstream.assign]]</a>
578
-
579
- ``` cpp
580
- basic_ifstream& operator=(basic_ifstream&& rhs);
581
- ```
582
-
583
- *Effects:* Move assigns the base and members of `*this` from the base
584
- and corresponding members of `rhs`.
585
-
586
- *Returns:* `*this`.
587
-
588
- ``` cpp
589
- void swap(basic_ifstream& rhs);
590
- ```
591
-
592
- *Effects:* Exchanges the state of `*this` and `rhs` by calling
593
- `basic_istream<charT,traits>::swap(rhs)` and `sb.swap(rhs.sb)`.
594
-
595
- ``` cpp
596
- template <class charT, class traits>
597
- void swap(basic_ifstream<charT, traits>& x,
598
- basic_ifstream<charT, traits>& y);
599
- ```
600
-
601
- *Effects:* `x.swap(y)`.
602
-
603
- #### Member functions <a id="ifstream.members">[[ifstream.members]]</a>
604
-
605
- ``` cpp
606
- basic_filebuf<charT,traits>* rdbuf() const;
607
- ```
608
-
609
- *Returns:* `const_cast<basic_filebuf<charT,traits>*>(&sb)`.
610
-
611
- ``` cpp
612
- bool is_open() const;
613
- ```
614
-
615
- *Returns:* `rdbuf()->is_open()`.
616
-
617
- ``` cpp
618
- void open(const char* s, ios_base::openmode mode = ios_base::in);
619
- ```
620
-
621
- *Effects:* Calls `rdbuf()->open(s, mode | ios_base::in)`. If that
622
- function does not return a null pointer calls `clear()`, otherwise calls
623
- `setstate(failbit)` (which may throw `ios_base::failure`
624
- ([[iostate.flags]])).
625
-
626
- ``` cpp
627
- void open(const string& s, ios_base::openmode mode = ios_base::in);
628
- ```
629
-
630
- *Effects:* calls `open(s.c_str(), mode)`.
631
-
632
- ``` cpp
633
- void close();
634
- ```
635
-
636
- *Effects:* Calls `rdbuf()->close()` and, if that function returns a null
637
- pointer, calls `setstate(failbit)` (which may throw
638
- `ios_base::failure` ([[iostate.flags]])).
639
-
640
- #### Class template `basic_ofstream` <a id="ofstream">[[ofstream]]</a>
641
-
642
- ``` cpp
643
- namespace std {
644
- template <class charT, class traits = char_traits<charT> >
645
- class basic_ofstream : public basic_ostream<charT,traits> {
646
- public:
647
- typedef charT char_type;
648
- typedef typename traits::int_type int_type;
649
- typedef typename traits::pos_type pos_type;
650
- typedef typename traits::off_type off_type;
651
- typedef traits traits_type;
652
-
653
- // [ofstream.cons] Constructors:
654
- basic_ofstream();
655
- explicit basic_ofstream(const char* s,
656
- ios_base::openmode mode = ios_base::out);
657
- explicit basic_ofstream(const string& s,
658
- ios_base::openmode mode = ios_base::out);
659
- basic_ofstream(const basic_ofstream& rhs) = delete;
660
- basic_ofstream(basic_ofstream&& rhs);
661
-
662
- // [ofstream.assign] Assign/swap:
663
- basic_ofstream& operator=(const basic_ofstream& rhs) = delete;
664
- basic_ofstream& operator=(basic_ofstream&& rhs);
665
- void swap(basic_ofstream& rhs);
666
-
667
- // [ofstream.members] Members:
668
- basic_filebuf<charT,traits>* rdbuf() const;
669
-
670
- bool is_open() const;
671
- void open(const char* s, ios_base::openmode mode = ios_base::out);
672
- void open(const string& s, ios_base::openmode mode = ios_base::out);
673
- void close();
674
- private:
675
- basic_filebuf<charT,traits> sb; // exposition only
676
- };
677
-
678
- template <class charT, class traits>
679
- void swap(basic_ofstream<charT, traits>& x,
680
- basic_ofstream<charT, traits>& y);
681
- }
682
- ```
683
-
684
- The class `basic_ofstream<charT, traits>` supports writing to named
685
- files. It uses a `basic_filebuf<{}charT, traits>` object to control the
686
- associated sequence. For the sake of exposition, the maintained data is
687
- presented here as:
688
-
689
- - `sb`, the `filebuf` object.
690
-
691
- #### `basic_ofstream` constructors <a id="ofstream.cons">[[ofstream.cons]]</a>
692
-
693
- ``` cpp
694
- basic_ofstream();
695
- ```
696
-
697
- *Effects:* Constructs an object of class `basic_ofstream<charT,traits>`,
698
- initializing the base class with `basic_ostream(&sb)` and initializing
699
- `sb` with `basic_filebuf<charT,traits>())` ([[ostream.cons]],
700
- [[filebuf.cons]]).
701
-
702
- ``` cpp
703
- explicit basic_ofstream(const char* s,
704
- ios_base::openmode mode = ios_base::out);
705
- ```
706
-
707
- *Effects:* Constructs an object of class `basic_ofstream<charT,traits>`,
708
- initializing the base class with `basic_ostream(&sb)` and initializing
709
- `sb` with `basic_filebuf<charT,traits>())` ([[ostream.cons]],
710
- [[filebuf.cons]]), then calls `rdbuf()->open(s, mode|ios_base::out)`. If
711
- that function returns a null pointer, calls `setstate(failbit)`.
712
-
713
- ``` cpp
714
- explicit basic_ofstream(const string& s,
715
- ios_base::openmode mode = ios_base::out);
716
- ```
717
-
718
- *Effects:* the same as `basic_ofstream(s.c_str(), mode);`
719
-
720
- ``` cpp
721
- basic_ofstream(basic_ofstream&& rhs);
722
- ```
723
-
724
- *Effects:* Move constructs from the rvalue `rhs`. This is accomplished
725
- by move constructing the base class, and the contained `basic_filebuf`.
726
- Next `basic_ostream<charT,traits>::set_rdbuf(&sb)` is called to install
727
- the contained `basic_filebuf`.
728
-
729
- #### Assign and swap <a id="ofstream.assign">[[ofstream.assign]]</a>
730
-
731
- ``` cpp
732
- basic_ofstream& operator=(basic_ofstream&& rhs);
733
- ```
734
-
735
- *Effects:* Move assigns the base and members of `*this` from the base
736
- and corresponding members of `rhs`.
737
-
738
- *Returns:* `*this`.
739
-
740
- ``` cpp
741
- void swap(basic_ofstream& rhs);
742
- ```
743
-
744
- *Effects:* Exchanges the state of `*this` and `rhs` by calling
745
- `basic_ostream<charT,traits>::swap(rhs)` and `sb.swap(rhs.sb)`.
746
-
747
- ``` cpp
748
- template <class charT, class traits>
749
- void swap(basic_ofstream<charT, traits>& x,
750
- basic_ofstream<charT, traits>& y);
751
- ```
752
-
753
- *Effects:* `x.swap(y)`.
754
-
755
- #### Member functions <a id="ofstream.members">[[ofstream.members]]</a>
756
-
757
- ``` cpp
758
- basic_filebuf<charT,traits>* rdbuf() const;
759
- ```
760
-
761
- *Returns:* `const_cast<basic_filebuf<charT,traits>*>(&sb)`.
762
-
763
- ``` cpp
764
- bool is_open() const;
765
- ```
766
-
767
- *Returns:* `rdbuf()->is_open()`.
768
-
769
- ``` cpp
770
- void open(const char* s, ios_base::openmode mode = ios_base::out);
771
- ```
772
-
773
- *Effects:* Calls `rdbuf()->open(s, mode | ios_base::out)`. If that
774
- function does not return a null pointer calls `clear()`, otherwise calls
775
- `setstate(failbit)` (which may throw `ios_base::failure`
776
- ([[iostate.flags]])).
777
-
778
- ``` cpp
779
- void close();
780
- ```
781
-
782
- *Effects:* Calls `rdbuf()->close()` and, if that function fails (returns
783
- a null pointer), calls `setstate(failbit)` (which may throw
784
- `ios_base::failure` ([[iostate.flags]])).
785
-
786
- ``` cpp
787
- void open(const string& s, ios_base::openmode mode = ios_base::out);
788
- ```
789
-
790
- *Effects:* calls `open(s.c_str(), mode);`
791
-
792
- #### Class template `basic_fstream` <a id="fstream">[[fstream]]</a>
793
-
794
- ``` cpp
795
- namespace std {
796
- template <class charT, class traits=char_traits<charT> >
797
- class basic_fstream
798
- : public basic_iostream<charT,traits> {
799
-
800
- public:
801
- typedef charT char_type;
802
- typedef typename traits::int_type int_type;
803
- typedef typename traits::pos_type pos_type;
804
- typedef typename traits::off_type off_type;
805
- typedef traits traits_type;
806
-
807
- // constructors/destructor
808
- basic_fstream();
809
- explicit basic_fstream(const char* s,
810
- ios_base::openmode mode = ios_base::in|ios_base::out);
811
- explicit basic_fstream(const string& s,
812
- ios_base::openmode mode = ios_base::in|ios_base::out);
813
- basic_fstream(const basic_fstream& rhs) = delete;
814
- basic_fstream(basic_fstream&& rhs);
815
-
816
- // [fstream.assign] Assign/swap:
817
- basic_fstream& operator=(const basic_fstream& rhs) = delete;
818
- basic_fstream& operator=(basic_fstream&& rhs);
819
- void swap(basic_fstream& rhs);
820
-
821
- // Members:
822
- basic_filebuf<charT,traits>* rdbuf() const;
823
- bool is_open() const;
824
- void open(const char* s,
825
- ios_base::openmode mode = ios_base::in|ios_base::out);
826
- void open(const string& s,
827
- ios_base::openmode mode = ios_base::in|ios_base::out);
828
- void close();
829
-
830
- private:
831
- basic_filebuf<charT,traits> sb; // exposition only
832
- };
833
-
834
- template <class charT, class traits>
835
- void swap(basic_fstream<charT, traits>& x,
836
- basic_fstream<charT, traits>& y);
837
- }
838
- ```
839
-
840
- The class template `basic_fstream<charT,traits>` supports reading and
841
- writing from named files. It uses a `basic_filebuf<charT,traits>` object
842
- to control the associated sequences. For the sake of exposition, the
843
- maintained data is presented here as:
844
-
845
- - `sb`, the `basic_filebuf` object.
846
-
847
- #### `basic_fstream` constructors <a id="fstream.cons">[[fstream.cons]]</a>
848
-
849
- ``` cpp
850
- basic_fstream();
851
- ```
852
-
853
- *Effects:* Constructs an object of class `basic_fstream<charT,traits>`,
854
- initializing the base class with `basic_iostream(&sb)` and initializing
855
- `sb` with `basic_filebuf<charT,traits>()`.
856
-
857
- ``` cpp
858
- explicit basic_fstream(const char* s,
859
- ios_base::openmode mode = ios_base::in|ios_base::out);
860
- ```
861
-
862
- *Effects:* Constructs an object of class `basic_fstream<charT, traits>`,
863
- initializing the base class with `basic_iostream(&sb)` and initializing
864
- `sb` with `basic_filebuf<charT, traits>()`. Then calls
865
- `rdbuf()->open(s, mode)`. If that function returns a null pointer, calls
866
- `setstate(failbit)`.
867
-
868
- ``` cpp
869
- explicit basic_fstream(const string& s,
870
- ios_base::openmode mode = ios_base::in|ios_base::out);
871
- ```
872
-
873
- *Effects:* the same as `basic_fstream(s.c_str(), mode);`
874
-
875
- ``` cpp
876
- basic_fstream(basic_fstream&& rhs);
877
- ```
878
-
879
- *Effects:* Move constructs from the rvalue `rhs`. This is accomplished
880
- by move constructing the base class, and the contained `basic_filebuf`.
881
- Next `basic_istream<charT,traits>::set_rdbuf(&sb)` is called to install
882
- the contained `basic_filebuf`.
883
-
884
- #### Assign and swap <a id="fstream.assign">[[fstream.assign]]</a>
885
-
886
- ``` cpp
887
- basic_fstream& operator=(basic_fstream&& rhs);
888
- ```
889
-
890
- *Effects:* Move assigns the base and members of `*this` from the base
891
- and corresponding members of `rhs`.
892
-
893
- *Returns:* `*this`.
894
-
895
- ``` cpp
896
- void swap(basic_fstream& rhs);
897
- ```
898
-
899
- *Effects:* Exchanges the state of `*this` and `rhs` by calling
900
- `basic_iostream<charT,traits>::swap(rhs)` and `sb.swap(rhs.sb)`.
901
-
902
- ``` cpp
903
- template <class charT, class traits>
904
- void swap(basic_fstream<charT, traits>& x,
905
- basic_fstream<charT, traits>& y);
906
- ```
907
-
908
- *Effects:* `x.swap(y)`.
909
-
910
- #### Member functions <a id="fstream.members">[[fstream.members]]</a>
911
-
912
- ``` cpp
913
- basic_filebuf<charT,traits>* rdbuf() const;
914
- ```
915
-
916
- *Returns:* `const_cast<basic_filebuf<charT,traits>*>(&sb)`.
917
-
918
- ``` cpp
919
- bool is_open() const;
920
- ```
921
-
922
- *Returns:* `rdbuf()->is_open()`.
923
-
924
- ``` cpp
925
- void open(const char* s,
926
- ios_base::openmode mode = ios_base::in|ios_base::out);
927
- ```
928
-
929
- *Effects:* Calls `rdbuf()->open(s,mode)`. If that function does not
930
- return a null pointer calls `clear()`, otherwise calls
931
- `setstate(failbit)`, (which may throw
932
- `ios_base::failure`) ([[iostate.flags]]).
933
-
934
- ``` cpp
935
- void open(const string& s,
936
- ios_base::openmode mode = ios_base::in|ios_base::out);
937
- ```
938
-
939
- *Effects:* calls `open(s.c_str(), mode);`
940
-
941
- ``` cpp
942
- void close();
943
- ```
944
-
945
- *Effects:* Calls `rdbuf()->close()` and, if that function returns
946
- returns a null pointer, calls `setstate(failbit)` ([[iostate.flags]])
947
- (which may throw `ios_base::failure`).
948
-