fstream

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

Generated on Tue Sep 7 10:05:04 2004 for libstdc++-v3 Source by doxygen 1.3.8