📄 cppio_details.html
字号:
<tr> <td>ios::trunc</td> <td>overwrite the existing file</td> </tr> </table> <p>If open() fails, the resulting stream will evaluate to false when used in a Boolean expression. For example:</p><pre> ifstream inputStream("file.txt"); if( !inputStream ) { cerr << "Error opening input stream" << endl; return; }</pre> <i>Related topics:</i><br> <strong><a href="#close">close()</a>, <a href="#Constructors">fstream(), ifstream(), ofstream()</a>,</strong> <hr> <h2><a name="peek">peek</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> int peek();</pre> </td> </tr> </table> <p>The function peek() is used with input streams, and returns the next character in the stream or EOF if the end of file is read. peek() does not remove the character from the stream.</p> <i>Related topics:</i><br> <strong><a href="#get">get()</a>, <a href="#putback">putback()</a></strong> <hr> <h2><a name="precision">precision</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> streamsize precision(); streamsize precision( streamsize p );</pre> </td> </tr> </table> <p>The precision() function either sets or returns the current number of digits that is displayed for floating-point variables. For example, the following code:</p><pre> float num = 314.15926535; cout.precision( 5 ); cout << num;</pre> <p>displays</p><pre> 314.16</pre> <i>Related topics:</i><br> <strong><a href="#width">width()</a>, <a href="#fill">fill()</a></strong> <hr> <h2><a name="put">put</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> ostream &put( char ch );</pre> </td> </tr> </table> <p>The function put() is used with output streams, and writes the character <i>ch</i> to the stream.</p> <i>Related topics:</i><br> <strong><a href="#write">write()</a>, <a href="#get">get()</a></strong> <hr> <h2><a name="putback">putback</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> istream &putback( char ch );</pre> </td> </tr> </table> <p>The putback() function is used with input streams, and returns the previously-read character <i>ch</i> to the input stream.</p> <i>Related topics:</i><br> <strong><a href="#peek">peek()</a></strong> <hr> <h2><a name="rdstate">rdstate</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> iostate rdstate();</pre> </td> </tr> </table> <p>The rdstate() function returns the status of the current stream. The <strong>iostate</strong> object has the these flags:</p> <table> <tr> <th>Flag</th> <th>Meaning</th> </tr> <tr bgcolor="#eeeeff"> <td>badbit</td> <td>a fatal error has occurred</td> </tr> <tr> <td>eofbit</td> <td>EOF has been found</td> </tr> <tr bgcolor="#eeeeff"> <td>failbit</td> <td>a nonfatal error has occurred</td> </tr> <tr> <td>goodbit</td> <td>no errors have occurred</td> </tr> </table> <p><i>Related topics:</i><br> <strong><a href="#eof">eof()</a>, <a href="#good">good()</a>, <a href="#bad">bad()</a>, <a href= "#clear">clear()</a>, <a href="#fail">fail()</a></strong></p> <hr> <h2><a name="read">read</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> istream &read( char *buffer, streamsize num );</pre> </td> </tr> </table> <p>The function read() is used with input streams, and reads <i>num</i> bytes from the stream before placing them in <i>buffer</i>. If EOF is encountered, read() stops, leaving however many bytes it put into <i>buffer</i> as they are. For example:</p><pre> struct { int height; int width; } rectangle; input_file.read( (char *)(&rectangle), sizeof(rectangle) ); if( input_file.bad() ) { cerr << "Error reading data" << endl; exit( 0 ); }</pre> <i>Related topics:</i><br> <strong><a href="#gcount">gcount()</a>, <a href="#get">get()</a>, <a href="#getline">getline()</a>, <a href="#write">write()</a></strong> <hr> <h2><a name="seekg">seekg</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> istream &seekg( off_type offset, ios::seekdir origin ); istream &seekg( pos_type position );</pre> </td> </tr> </table> <p>The function seekg() is used with input streams, and it repositions the "get" pointer for the current stream to <i>offset</i> bytes away from <i>origin</i>, or places the "get" pointer at <i>position</i>.</p> <i>Related topics:</i><br> <strong><a href="#seekp">seekp()</a>, <a href="#tellg">tellg()</a>, <a href="#tellp">tellp()</a></strong> <hr> <h2><a name="seekp">seekp</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> ostream &seekp( off_type offset, ios::seekdir origin ); ostream &seekp( pos_type position );</pre> </td> </tr> </table> <p>The seekp() function is used with output streams, but is otherwise very similar to <a href= "#seekg">seekg()</a>.</p> <i>Related topics:</i><br> <strong><a href="#seekg">seekg()</a>, <a href="#tellg">tellg()</a>, <a href="#tellp">tellp()</a></strong> <hr> <h2><a name="setf">setf</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> fmtflags setf( fmtflags flags ); fmtflags setf( fmtflags flags, fmtflags needed );</pre> </td> </tr> </table> <p>The function setf() sets the <a href="cppio_flags.html">formatting flags</a> of the current stream to <i>flags</i>. The optional <i>needed</i> lets only the flags that are in both <i>flags</i> and <i>needed</i> be set. The return value is the previous configuration of flags. For example:</p><pre> int number = 0x3FF; cout.setf( ios::dec ); cout << "Decimal: " << number << endl; cout.unsetf( ios::dec ); cout.setf( ios::hex ); cout << "Hexadecimal: " << number << endl;</pre> <p>Note that the preceding code is functionally identical to:</p><pre> int number = 0x3FF; cout << "Decimal: " << number << endl << hex << "Hexadecimal: " << number << dec << endl;</pre> <p>thanks to <a href="cppio_flags.html">manipulators</a>.</p> <i>Related topics:</i><br> <strong><a href="#flags">flags()</a>, <a href="#unsetf">unsetf()</a></strong> <hr> <h2><a name="sync_with_stdio">sync_with_stdio</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> static bool sync_with_stdio( bool sync=true );</pre> </td> </tr> </table> <p>The sync_with_stdio() function allows you to turn on and off the ability for the C++ I/O system to work with the C I/O system.</p> <hr> <h2><a name="tellg">tellg</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> pos_type tellg();</pre> </td> </tr> </table> <p>The tellg() function is used with input streams, and returns the current "get" position of the pointer in the stream.</p> <i>Related topics:</i><br> <strong><a href="#seekg">seekg()</a>, <a href="#seekp">seekp()</a>, <a href="#tellp">tellp()</a></strong> <hr> <h2><a name="tellp">tellp</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> pos_type tellp();</pre> </td> </tr> </table> <p>The tellp() function is used with output streams, and returns the current "put" position of the pointer in the stream. For example, the following code displays the file pointer as it writes to a stream:</p><pre> string s("In Xanadu did Kubla Khan..."); ofstream fout("output.txt"); for( int i=0; i < s.length(); i++ ) { cout << "File pointer: " << fout.tellp(); fout.put( s[i] ); cout << " " << s[i] << endl; } fout.close();</pre> <i>Related topics:</i><br> <strong><a href="#seekg">seekg()</a>, <a href="#seekp">seekp()</a>, <a href="#tellg">tellg()</a></strong> <hr> <h2><a name="unsetf">unsetf</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void unsetf( fmtflags flags );</pre> </td> </tr> </table> <p>The function unsetf() is used to clear the given <i>flags</i> associated with the current stream. <a href="cppio_flags.html">What flags?</a></p> <i>Related topics:</i><br> <strong><a href="#setf">setf()</a>, <a href="#flags">flags()</a></strong> <hr> <h2><a name="width">width</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> int width(); int width( int w );</pre> </td> </tr> </table> <p>The function width() returns the current width. The optional <i>w</i> can be used to set the width. Width is defined as the minimum number of characters to display with each output. For example:</p><pre> cout.width( 5 ); cout << "2";</pre> <p>displays</p><pre> 2</pre> <p>(that's four spaces followed by a '2')</p> <i>Related topics:</i><br> <strong><a href="#precision">precision()</a>, <a href="#fill">fill()</a></strong> <hr> <h2><a name="write">write</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> ostream &write( const char *buffer, streamsize num );</pre> </td> </tr> </table> <p>The write() function is used with output streams, and writes <i>num</i> bytes from <i>buffer</i> to the current output stream.</p> <i>Related topics:</i><br> <strong><a href="#read">read()</a>, <a href="#put">put()</a></strong> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -