00001 // File based streams -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 // 00032 // ISO C++ 14882: 27.8 File-based streams 00033 // 00034 00035 /** @file fstream 00036 * This is a Standard C++ Library header. 00037 */ 00038 00039 #ifndef _GLIBCXX_FSTREAM 00040 #define _GLIBCXX_FSTREAM 1 00041 00042 #pragma GCC system_header 00043 00044 #include <istream> 00045 #include <ostream> 00046 #include <locale> // For codecvt 00047 #include <cstdio> // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ 00048 #include <bits/basic_file.h> 00049 #include <bits/gthr.h> 00050 00051 namespace std 00052 { 00053 // [27.8.1.1] template class basic_filebuf 00054 /** 00055 * @brief The actual work of input and output (for files). 00056 * 00057 * This class associates both its input and output sequence with an 00058 * external disk file, and maintains a joint file position for both 00059 * sequences. Many of its sematics are described in terms of similar 00060 * behavior in the Standard C Library's @c FILE streams. 00061 */ 00062 // Requirements on traits_type, specific to this class: 00063 // traits_type::pos_type must be fpos<traits_type::state_type> 00064 // traits_type::off_type must be streamoff 00065 // traits_type::state_type must be Assignable and DefaultConstructable, 00066 // and traits_type::state_type() must be the initial state for codecvt. 00067 template<typename _CharT, typename _Traits> 00068 class basic_filebuf : public basic_streambuf<_CharT, _Traits> 00069 { 00070 public: 00071 // Types: 00072 typedef _CharT char_type; 00073 typedef _Traits traits_type; 00074 typedef typename traits_type::int_type int_type; 00075 typedef typename traits_type::pos_type pos_type; 00076 typedef typename traits_type::off_type off_type; 00077 00078 typedef basic_streambuf<char_type, traits_type> __streambuf_type; 00079 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00080 typedef __basic_file<char> __file_type; 00081 typedef typename traits_type::state_type __state_type; 00082 typedef codecvt<char_type, char, __state_type> __codecvt_type; 00083 00084 friend class ios_base; // For sync_with_stdio. 00085 00086 protected: 00087 // Data Members: 00088 // MT lock inherited from libio or other low-level io library. 00089 __c_lock _M_lock; 00090 00091 // External buffer. 00092 __file_type _M_file; 00093 00094 /** 00095 * @if maint 00096 * Place to stash in || out || in | out settings for current filebuf. 00097 * @endif 00098 */ 00099 ios_base::openmode _M_mode; 00100 00101 // Beginning state type for codecvt. 00102 __state_type _M_state_beg; 00103 00104 // During output, the state that corresponds to pptr(), 00105 // during input, the state that corresponds to egptr() and 00106 // _M_ext_next. 00107 __state_type _M_state_cur; 00108 00109 // Not used for output. During input, the state that corresponds 00110 // to eback() and _M_ext_buf. 00111 __state_type _M_state_last; 00112 00113 /** 00114 * @if maint 00115 * Pointer to the beginning of internal buffer. 00116 * @endif 00117 */ 00118 char_type* _M_buf; 00119 00120 /** 00121 * @if maint 00122 * Actual size of internal buffer. This number is equal to the size 00123 * of the put area + 1 position, reserved for the overflow char of 00124 * a full area. 00125 * @endif 00126 */ 00127 size_t _M_buf_size; 00128 00129 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer. 00130 bool _M_buf_allocated; 00131 00132 /** 00133 * @if maint 00134 * _M_reading == false && _M_writing == false for 'uncommitted' mode; 00135 * _M_reading == true for 'read' mode; 00136 * _M_writing == true for 'write' mode; 00137 * 00138 * NB: _M_reading == true && _M_writing == true is unused. 00139 * @endif 00140 */ 00141 bool _M_reading; 00142 bool _M_writing; 00143 00144 //@{ 00145 /** 00146 * @if maint 00147 * Necessary bits for putback buffer management. 00148 * 00149 * @note pbacks of over one character are not currently supported. 00150 * @endif 00151 */ 00152 char_type _M_pback; 00153 char_type* _M_pback_cur_save; 00154 char_type* _M_pback_end_save; 00155 bool _M_pback_init; 00156 //@} 00157 00158 // Cached codecvt facet. 00159 const __codecvt_type* _M_codecvt; 00160 00161 /** 00162 * @if maint 00163 * Buffer for external characters. Used for input when 00164 * codecvt::always_noconv() == false. When valid, this corresponds 00165 * to eback(). 00166 * @endif 00167 */ 00168 char* _M_ext_buf; 00169 00170 /** 00171 * @if maint 00172 * Size of buffer held by _M_ext_buf. 00173 * @endif 00174 */ 00175 streamsize _M_ext_buf_size; 00176 00177 /** 00178 * @if maint 00179 * Pointers into the buffer held by _M_ext_buf that delimit a 00180 * subsequence of bytes that have been read but not yet converted. 00181 * When valid, _M_ext_next corresponds to egptr(). 00182 * @endif 00183 */ 00184 const char* _M_ext_next; 00185 char* _M_ext_end; 00186 00187 /** 00188 * @if maint 00189 * Initializes pback buffers, and moves normal buffers to safety. 00190 * Assumptions: 00191 * _M_in_cur has already been moved back 00192 * @endif 00193 */ 00194 void 00195 _M_create_pback() 00196 { 00197 if (!_M_pback_init) 00198 { 00199 _M_pback_cur_save = this->gptr(); 00200 _M_pback_end_save = this->egptr(); 00201 this->setg(&_M_pback, &_M_pback, &_M_pback + 1); 00202 _M_pback_init = true; 00203 } 00204 } 00205 00206 /** 00207 * @if maint 00208 * Deactivates pback buffer contents, and restores normal buffer. 00209 * Assumptions: 00210 * The pback buffer has only moved forward. 00211 * @endif 00212 */ 00213 void 00214 _M_destroy_pback() throw() 00215 { 00216 if (_M_pback_init) 00217 { 00218 // Length _M_in_cur moved in the pback buffer. 00219 _M_pback_cur_save += this->gptr() != this->eback(); 00220 this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save); 00221 _M_pback_init = false; 00222 } 00223 } 00224 00225 public: 00226 // Constructors/destructor: 00227 /** 00228 * @brief Does not open any files. 00229 * 00230 * The default constructor initializes the parent class using its 00231 * own default ctor. 00232 */ 00233 basic_filebuf(); 00234 00235 /** 00236 * @brief The destructor closes the file first. 00237 */ 00238 virtual 00239 ~basic_filebuf() 00240 { this->close(); } 00241 00242 // Members: 00243 /** 00244 * @brief Returns true if the external file is open. 00245 */ 00246 bool 00247 is_open() const throw() 00248 { return _M_file.is_open(); } 00249 00250 /** 00251 * @brief Opens an external file. 00252 * @param s The name of the file. 00253 * @param mode The open mode flags. 00254 * @return @c this on success, NULL on failure 00255 * 00256 * If a file is already open, this function immediately fails. 00257 * Otherwise it tries to open the file named @a s using the flags 00258 * given in @a mode. 00259 * 00260 * [Table 92 gives the relation between openmode combinations and the 00261 * equivalent fopen() flags, but the table has not been copied yet.] 00262 */ 00263 __filebuf_type* 00264 open(const char* __s, ios_base::openmode __mode); 00265 00266 /** 00267 * @brief Closes the currently associated file. 00268 * @return @c this on success, NULL on failure 00269 * 00270 * If no file is currently open, this function immediately fails. 00271 * 00272 * If a "put buffer area" exists, @c overflow(eof) is called to flush 00273 * all the characters. The file is then closed. 00274 * 00275 * If any operations fail, this function also fails. 00276 */ 00277 __filebuf_type* 00278 close() throw(); 00279 00280 protected: 00281 void 00282 _M_allocate_internal_buffer(); 00283 00284 void 00285 _M_destroy_internal_buffer() throw(); 00286 00287 // [27.8.1.4] overridden virtual functions 00288 virtual streamsize 00289 showmanyc(); 00290 00291 // Stroustrup, 1998, p. 628 00292 // underflow() and uflow() functions are called to get the next 00293 // charater from the real input source when the buffer is empty. 00294 // Buffered input uses underflow() 00295 00296 virtual int_type 00297 underflow(); 00298 00299 virtual int_type 00300 pbackfail(int_type __c = _Traits::eof()); 00301 00302 // Stroustrup, 1998, p 648 00303 // The overflow() function is called to transfer characters to the 00304 // real output destination when the buffer is full. A call to 00305 // overflow(c) outputs the contents of the buffer plus the 00306 // character c. 00307 // 27.5.2.4.5 00308 // Consume some sequence of the characters in the pending sequence. 00309 virtual int_type 00310 overflow(int_type __c = _Traits::eof()); 00311 00312 // Convert internal byte sequence to external, char-based 00313 // sequence via codecvt. 00314 bool 00315 _M_convert_to_external(char_type*, streamsize); 00316 00317 /** 00318 * @brief Manipulates the buffer. 00319 * @param s Pointer to a buffer area. 00320 * @param n Size of @a s. 00321 * @return @c this 00322 * 00323 * If no file has been opened, and both @a s and @a n are zero, then 00324 * the stream becomes unbuffered. Otherwise, @c s is used as a 00325 * buffer; see 00326 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 00327 * for more. 00328 */ 00329 virtual __streambuf_type* 00330 setbuf(char_type* __s, streamsize __n); 00331 00332 virtual pos_type 00333 seekoff(off_type __off, ios_base::seekdir __way, 00334 ios_base::openmode __mode = ios_base::in | ios_base::out); 00335 00336 virtual pos_type 00337 seekpos(pos_type __pos, 00338 ios_base::openmode __mode = ios_base::in | ios_base::out); 00339 00340 // Common code for seekoff and seekpos 00341 pos_type 00342 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state); 00343 00344 virtual int 00345 sync(); 00346 00347 virtual void 00348 imbue(const locale& __loc); 00349 00350 virtual streamsize 00351 xsgetn(char_type* __s, streamsize __n); 00352 00353 virtual streamsize 00354 xsputn(const char_type* __s, streamsize __n); 00355 00356 // Flushes output buffer, then writes unshift sequence. 00357 bool 00358 _M_terminate_output(); 00359 00360 /** 00361 * @if maint 00362 * This function sets the pointers of the internal buffer, both get 00363 * and put areas. Typically: 00364 * 00365 * __off == egptr() - eback() upon underflow/uflow ('read' mode); 00366 * __off == 0 upon overflow ('write' mode); 00367 * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode). 00368 * 00369 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size 00370 * reflects the actual allocated memory and the last cell is reserved 00371 * for the overflow char of a full put area. 00372 * @endif 00373 */ 00374 void 00375 _M_set_buffer(streamsize __off) 00376 { 00377 const bool __testin = _M_mode & ios_base::in; 00378 const bool __testout = _M_mode & ios_base::out; 00379 00380 if (__testin && __off > 0) 00381 this->setg(_M_buf, _M_buf, _M_buf + __off); 00382 else 00383 this->setg(_M_buf, _M_buf, _M_buf); 00384 00385 if (__testout && __off == 0 && _M_buf_size > 1 ) 00386 this->setp(_M_buf, _M_buf + _M_buf_size - 1); 00387 else 00388 this->setp(NULL, NULL); 00389 } 00390 }; 00391 00392 // [27.8.1.5] Template class basic_ifstream 00393 /** 00394 * @brief Controlling input for files. 00395 * 00396 * This class supports reading from named files, using the inherited 00397 * functions from std::basic_istream. To control the associated 00398 * sequence, an instance of std::basic_filebuf is used, which this page 00399 * refers to as @c sb. 00400 */ 00401 template<typename _CharT, typename _Traits> 00402 class basic_ifstream : public basic_istream<_CharT, _Traits> 00403 { 00404 public: 00405 // Types: 00406 typedef _CharT char_type; 00407 typedef _Traits traits_type; 00408 typedef typename traits_type::int_type int_type; 00409 typedef typename traits_type::pos_type pos_type; 00410 typedef typename traits_type::off_type off_type; 00411 00412 // Non-standard types: 00413 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00414 typedef basic_istream<char_type, traits_type> __istream_type; 00415 00416 private: 00417 __filebuf_type _M_filebuf; 00418 00419 public: 00420 // Constructors/Destructors: 00421 /** 00422 * @brief Default constructor. 00423 * 00424 * Initializes @c sb using its default constructor, and passes 00425 * @c &sb to the base class initializer. Does not open any files 00426 * (you haven't given it a filename to open). 00427 */ 00428 basic_ifstream() : __istream_type(), _M_filebuf() 00429 { this->init(&_M_filebuf); } 00430 00431 /** 00432 * @brief Create an input file stream. 00433 * @param s Null terminated string specifying the filename. 00434 * @param mode Open file in specified mode (see std::ios_base). 00435 * 00436 * @c ios_base::in is automatically included in @a mode. 00437 * 00438 * Tip: When using std::string to hold the filename, you must use 00439 * .c_str() before passing it to this constructor. 00440 */ 00441 explicit 00442 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in) 00443 : __istream_type(), _M_filebuf() 00444 { 00445 this->init(&_M_filebuf); 00446 this->open(__s, __mode); 00447 } 00448 00449 /** 00450 * @brief The destructor does nothing. 00451 * 00452 * The file is closed by the filebuf object, not the formatting 00453 * stream. 00454 */ 00455 ~basic_ifstream() 00456 { } 00457 00458 // Members: 00459 /** 00460 * @brief Accessing the underlying buffer. 00461 * @return The current basic_filebuf buffer. 00462 * 00463 * This hides both signatures of std::basic_ios::rdbuf(). 00464 */ 00465 __filebuf_type* 00466 rdbuf() const 00467 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00468 00469 /** 00470 * @brief Wrapper to test for an open file. 00471 * @return @c rdbuf()->is_open() 00472 */ 00473 bool 00474 is_open() 00475 { return _M_filebuf.is_open(); } 00476 00477 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00478 // 365. Lack of const-qualification in clause 27 00479 bool 00480 is_open() const 00481 { return _M_filebuf.is_open(); } 00482 00483 /** 00484 * @brief Opens an external file. 00485 * @param s The name of the file. 00486 * @param mode The open mode flags. 00487 * 00488 * Calls @c std::basic_filebuf::open(s,mode|in). If that function 00489 * fails, @c failbit is set in the stream's error state. 00490 * 00491 * Tip: When using std::string to hold the filename, you must use 00492 * .c_str() before passing it to this constructor. 00493 */ 00494 void 00495 open(const char* __s, ios_base::openmode __mode = ios_base::in) 00496 { 00497 if (!_M_filebuf.open(__s, __mode | ios_base::in)) 00498 this->setstate(ios_base::failbit); 00499 else 00500 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00501 // 409. Closing an fstream should clear error state 00502 this->clear(); 00503 } 00504 00505 /** 00506 * @brief Close the file. 00507 * 00508 * Calls @c std::basic_filebuf::close(). If that function 00509 * fails, @c failbit is set in the stream's error state. 00510 */ 00511 void 00512 close() 00513 { 00514 if (!_M_filebuf.close()) 00515 this->setstate(ios_base::failbit); 00516 } 00517 }; 00518 00519 00520 // [27.8.1.8] Template class basic_ofstream 00521 /** 00522 * @brief Controlling output for files. 00523 * 00524 * This class supports reading from named files, using the inherited 00525 * functions from std::basic_ostream. To control the associated 00526 * sequence, an instance of std::basic_filebuf is used, which this page 00527 * refers to as @c sb. 00528 */ 00529 template<typename _CharT, typename _Traits> 00530 class basic_ofstream : public basic_ostream<_CharT,_Traits> 00531 { 00532 public: 00533 // Types: 00534 typedef _CharT char_type; 00535 typedef _Traits traits_type; 00536 typedef typename traits_type::int_type int_type; 00537 typedef typename traits_type::pos_type pos_type; 00538 typedef typename traits_type::off_type off_type; 00539 00540 // Non-standard types: 00541 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00542 typedef basic_ostream<char_type, traits_type> __ostream_type; 00543 00544 private: 00545 __filebuf_type _M_filebuf; 00546 00547 public: 00548 // Constructors: 00549 /** 00550 * @brief Default constructor. 00551 * 00552 * Initializes @c sb using its default constructor, and passes 00553 * @c &sb to the base class initializer. Does not open any files 00554 * (you haven't given it a filename to open). 00555 */ 00556 basic_ofstream(): __ostream_type(), _M_filebuf() 00557 { this->init(&_M_filebuf); } 00558 00559 /** 00560 * @brief Create an output file stream. 00561 * @param s Null terminated string specifying the filename. 00562 * @param mode Open file in specified mode (see std::ios_base). 00563 * 00564 * @c ios_base::out|ios_base::trunc is automatically included in 00565 * @a mode. 00566 * 00567 * Tip: When using std::string to hold the filename, you must use 00568 * .c_str() before passing it to this constructor. 00569 */ 00570 explicit 00571 basic_ofstream(const char* __s, 00572 ios_base::openmode __mode = ios_base::out|ios_base::trunc) 00573 : __ostream_type(), _M_filebuf() 00574 { 00575 this->init(&_M_filebuf); 00576 this->open(__s, __mode); 00577 } 00578 00579 /** 00580 * @brief The destructor does nothing. 00581 * 00582 * The file is closed by the filebuf object, not the formatting 00583 * stream. 00584 */ 00585 ~basic_ofstream() 00586 { } 00587 00588 // Members: 00589 /** 00590 * @brief Accessing the underlying buffer. 00591 * @return The current basic_filebuf buffer. 00592 * 00593 * This hides both signatures of std::basic_ios::rdbuf(). 00594 */ 00595 __filebuf_type* 00596 rdbuf() const 00597 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00598 00599 /** 00600 * @brief Wrapper to test for an open file. 00601 * @return @c rdbuf()->is_open() 00602 */ 00603 bool 00604 is_open() 00605 { return _M_filebuf.is_open(); } 00606 00607 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00608 // 365. Lack of const-qualification in clause 27 00609 bool 00610 is_open() const 00611 { return _M_filebuf.is_open(); } 00612 00613 /** 00614 * @brief Opens an external file. 00615 * @param s The name of the file. 00616 * @param mode The open mode flags. 00617 * 00618 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that 00619 * function fails, @c failbit is set in the stream's error state. 00620 * 00621 * Tip: When using std::string to hold the filename, you must use 00622 * .c_str() before passing it to this constructor. 00623 */ 00624 void 00625 open(const char* __s, 00626 ios_base::openmode __mode = ios_base::out | ios_base::trunc) 00627 { 00628 if (!_M_filebuf.open(__s, __mode | ios_base::out)) 00629 this->setstate(ios_base::failbit); 00630 else 00631 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00632 // 409. Closing an fstream should clear error state 00633 this->clear(); 00634 } 00635 00636 /** 00637 * @brief Close the file. 00638 * 00639 * Calls @c std::basic_filebuf::close(). If that function 00640 * fails, @c failbit is set in the stream's error state. 00641 */ 00642 void 00643 close() 00644 { 00645 if (!_M_filebuf.close()) 00646 this->setstate(ios_base::failbit); 00647 } 00648 }; 00649 00650 00651 // [27.8.1.11] Template class basic_fstream 00652 /** 00653 * @brief Controlling intput and output for files. 00654 * 00655 * This class supports reading from and writing to named files, using 00656 * the inherited functions from std::basic_iostream. To control the 00657 * associated sequence, an instance of std::basic_filebuf is used, which 00658 * this page refers to as @c sb. 00659 */ 00660 template<typename _CharT, typename _Traits> 00661 class basic_fstream : public basic_iostream<_CharT, _Traits> 00662 { 00663 public: 00664 // Types: 00665 typedef _CharT char_type; 00666 typedef _Traits traits_type; 00667 typedef typename traits_type::int_type int_type; 00668 typedef typename traits_type::pos_type pos_type; 00669 typedef typename traits_type::off_type off_type; 00670 00671 // Non-standard types: 00672 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00673 typedef basic_ios<char_type, traits_type> __ios_type; 00674 typedef basic_iostream<char_type, traits_type> __iostream_type; 00675 00676 private: 00677 __filebuf_type _M_filebuf; 00678 00679 public: 00680 // Constructors/destructor: 00681 /** 00682 * @brief Default constructor. 00683 * 00684 * Initializes @c sb using its default constructor, and passes 00685 * @c &sb to the base class initializer. Does not open any files 00686 * (you haven't given it a filename to open). 00687 */ 00688 basic_fstream() 00689 : __iostream_type(), _M_filebuf() 00690 { this->init(&_M_filebuf); } 00691 00692 /** 00693 * @brief Create an input/output file stream. 00694 * @param s Null terminated string specifying the filename. 00695 * @param mode Open file in specified mode (see std::ios_base). 00696 * 00697 * Tip: When using std::string to hold the filename, you must use 00698 * .c_str() before passing it to this constructor. 00699 */ 00700 explicit 00701 basic_fstream(const char* __s, 00702 ios_base::openmode __mode = ios_base::in | ios_base::out) 00703 : __iostream_type(NULL), _M_filebuf() 00704 { 00705 this->init(&_M_filebuf); 00706 this->open(__s, __mode); 00707 } 00708 00709 /** 00710 * @brief The destructor does nothing. 00711 * 00712 * The file is closed by the filebuf object, not the formatting 00713 * stream. 00714 */ 00715 ~basic_fstream() 00716 { } 00717 00718 // Members: 00719 /** 00720 * @brief Accessing the underlying buffer. 00721 * @return The current basic_filebuf buffer. 00722 * 00723 * This hides both signatures of std::basic_ios::rdbuf(). 00724 */ 00725 __filebuf_type* 00726 rdbuf() const 00727 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00728 00729 /** 00730 * @brief Wrapper to test for an open file. 00731 * @return @c rdbuf()->is_open() 00732 */ 00733 bool 00734 is_open() 00735 { return _M_filebuf.is_open(); } 00736 00737 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00738 // 365. Lack of const-qualification in clause 27 00739 bool 00740 is_open() const 00741 { return _M_filebuf.is_open(); } 00742 00743 /** 00744 * @brief Opens an external file. 00745 * @param s The name of the file. 00746 * @param mode The open mode flags. 00747 * 00748 * Calls @c std::basic_filebuf::open(s,mode). If that 00749 * function fails, @c failbit is set in the stream's error state. 00750 * 00751 * Tip: When using std::string to hold the filename, you must use 00752 * .c_str() before passing it to this constructor. 00753 */ 00754 void 00755 open(const char* __s, 00756 ios_base::openmode __mode = ios_base::in | ios_base::out) 00757 { 00758 if (!_M_filebuf.open(__s, __mode)) 00759 this->setstate(ios_base::failbit); 00760 else 00761 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00762 // 409. Closing an fstream should clear error state 00763 this->clear(); 00764 } 00765 00766 /** 00767 * @brief Close the file. 00768 * 00769 * Calls @c std::basic_filebuf::close(). If that function 00770 * fails, @c failbit is set in the stream's error state. 00771 */ 00772 void 00773 close() 00774 { 00775 if (!_M_filebuf.close()) 00776 this->setstate(ios_base::failbit); 00777 } 00778 }; 00779 } // namespace std 00780 00781 #ifndef _GLIBCXX_EXPORT_TEMPLATE 00782 # include <bits/fstream.tcc> 00783 #endif 00784 00785 #endif /* _GLIBCXX_FSTREAM */