📄 cppio_details.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta name="generator" content="HTML Tidy for Linux/x86 (vers 1st October 2002), see www.w3.org"> <title>C++ I/O</title> </head> <body bgcolor="#ffffff"> <table width="100%" bgcolor="#eeeeff"> <tr> <td><a href="index.html">cppreference.com</a> -> <a href="cppio.html">C++ I/O</a> -> Details</td> </tr> </table> <h1>C++ I/O</h1> <hr> <h2><a name="Constructors">Constructors</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> fstream( const char *filename, openmode mode ); ifstream( const char *filename, openmode mode ); ofstream( const char *filename, openmode mode );</pre> </td> </tr> </table> <p>The fstream, ifstream, and ofstream objects are used to do file I/O. The optional <i>mode</i> defines how the file is to be opened, according to the <a href="#mode_flags">ios stream mode flags</a>. The optional <i>filename</i> specifies the file to be opened and associated with the stream. For example, the following code reads input data and appends the result to an output file.</p><pre> ifstream fin( "/tmp/data.txt" ); ofstream fout( "/tmp/results.txt", ios::app ); while( fin >> temp ) fout << temp + 2 << endl; fin.close(); fout.close();</pre> <p>Input and output file streams can be used in a similar manner to C++ predefined I/O streams, <strong>cin</strong> and <strong>cout</strong>.</p> <i>Related topics:</i><br> <strong><a href="#close">close()</a>, <a href="#open">open()</a></strong> <hr> <h2><a name="bad">bad</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> bool bad();</pre> </td> </tr> </table> <p>The bad() function returns <strong>true</strong> if a fatal error with the current stream has occurred, <strong>false</strong> otherwise.</p> <i>Related topics:</i><br> <strong><a href="#good">good()</a></strong> <hr> <h2><a name="clear">clear</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void clear( iostate flags = goodbit );</pre> </td> </tr> </table> <p>The function clear() clears the <a href="cppio_flags.html">flags</a> associated with the current stream. The default flag is goodbit, which clears all flags. Otherwise, only <i>flags</i> are cleared.</p> <i>Related topics:</i><br> <strong><a href="#rdstate">rdstate()</a></strong> <hr> <h2><a name="close">close</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void close();</pre> </td> </tr> </table> <p>The close() function closes the associated file stream.</p> <i>Related topics:</i><br> <strong><a href="#open">open()</a></strong> <hr> <h2><a name="eof">eof</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> bool eof();</pre> </td> </tr> </table> <p>The function eof() returns <strong>true</strong> if the end of the associated input file has been reached, <strong>false</strong> otherwise. For example:</p><pre> char ch; ifstream fin( "temp.txt" ); while( !fin.eof() ) { fin >> ch; cout << ch; } fin.close();</pre> <i>Related topics:</i><br> <strong><a href="#bad">bad()</a>, <a href="#fail">fail()</a>, <a href="#good">good()</a>, <a href= "#rdstate">rdstate()</a>, <a href="#clear">clear()</a></strong> <hr> <h2><a name="fail">fail</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> bool fail();</pre> </td> </tr> </table> <p>The fail() function returns <strong>true</strong> if an error has occurred with the current stream, <strong>false</strong> otherwise.</p> <i>Related topics:</i><br> <strong><a href="#good">good()</a>, <a href="#eof">eof()</a>, <a href="#bad">bad()</a>, <a href= "#clear">clear()</a>, <a href="#rdstate">rdstate()</a></strong> <hr> <h2><a name="fill">fill</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> char fill(); char fill( char ch );</pre> </td> </tr> </table> <p>The function fill() either returns the current fill character, or sets the current fill character to <i>ch</i>. The fill character is defined as the character that is used for padding when a number is smaller than the specified <a href="#width">width</a>. The default fill character is the space character.</p> <i>Related topics:</i><br> <strong><a href="#precision">precision()</a>, <a href="#width">width()</a></strong> <hr> <h2><a name="flags">flags</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> fmtflags flags(); fmtflags flags( fmtflags f );</pre> </td> </tr> </table> <p>The flags() function either returns the <a href="cppio_flags.html">format flags</a> for the current stream, or sets the flags for the current stream to be <i>f</i>.</p> <i>Related topics:</i><br> <strong><a href="#unsetf">unsetf()</a>, <a href="#setf">setf()</a></strong> <hr> <h2><a name="flush">flush</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> ostream &flush();</pre> </td> </tr> </table> <p>The flush() function causes the buffer for the current output stream to be actually written out to the attached device. This function is useful for printing out debugging information, because sometimes programs abort before they have a chance to write their output buffers to the screen. Judicious use of flush() can ensure that all of your debugging statements actually get printed.</p> <i>Related topics:</i><br> <strong><a href="#put">put()</a>, <a href="#write">write()</a></strong> <hr> <h2><a name="gcount">gcount</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> streamsize gcount();</pre> </td> </tr> </table> <p>The function gcount() is used with input streams, and returns the number of characters read by the last input operation.</p> <i>Related topics:</i><br> <strong><a href="#get">get()</a>, <a href="#getline">getline()</a>, <a href="#read">read()</a></strong> <hr> <h2><a name="get">get</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> int get(); istream &get( char &ch ); istream &get( char *buffer, streamsize num ); istream &get( char *buffer, streamsize num, char delim ); istream &get( streambuf &buffer ); istream &get( streambuf &buffer, char delim );</pre> </td> </tr> </table> <p>The get() function is used with input streams, and either:</p> <ul> <li>reads a character and returns that value,</li> <li>reads a character and stores it as <i>ch</i>,</li> <li>reads characters into <i>buffer</i> until <i>num</i> - 1 characters have been read, or EOF or newline encountered,</li> <li>reads characters into <i>buffer</i> until <i>num</i> - 1 characters have been read, or EOF or the <i>delim</i> character encountered (<i>delim</i> is not read until next time),</li> <li>reads characters into <i>buffer</i> until a newline or EOF is encountered,</li> <li>or reads characters into <i>buffer</i> until a newline, EOF, or <i>delim</i> character is encountered (again, <i>delim</i> isn't read until the next get() ).</li> </ul> For example, the following code displays the contents of temp.txt, character by character: <br> <br> <pre> char ch; ifstream fin( "temp.txt" ); while( fin.get(ch) ) cout << ch; fin.close();</pre> <i>Related topics:</i><br> <strong><a href="#put">put()</a>, <a href="#read">read()</a>, <a href="#getline">getline()</a></strong> <hr> <h2><a name="getline">getline</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> istream &getline( char *buffer, streamsize num ); istream &getline( char *buffer, streamsize num, char delim );</pre> </td> </tr> </table> <p>The getline() function is used with input streams, and reads characters into <i>buffer</i> until either:</p> <ul> <li><i>num</i> - 1 characters have been read,</li> <li>a newline is encountered,</li> <li>an EOF is encountered,</li> <li>or, optionally, until the character <i>delim</i> is read. The <i>delim</i> character is not put into <i>buffer</i>.</li> </ul> <p>Those using a Microsoft compiler may experience getline() reading an extra character, and should consult <a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015">this page</a>. </p> <i>Related topics:</i><br> <strong><a href="#get">get()</a>, <a href="#read">read()</a></strong> <hr> <h2><a name="good">good</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> bool good();</pre> </td> </tr> </table> <p>The function good() returns <strong>true</strong> if no errors have occurred with the current stream, <strong>false</strong> otherwise.</p> <i>Related topics:</i><br> <strong><a href="#bad">bad()</a>, <a href="#fail">fail()</a>, <a href="#eof">eof()</a>, <a href= "#clear">clear()</a>, <a href="#rdstate">rdstate()</a></strong> <hr> <h2><a name="ignore">ignore</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> istream &ignore( streamsize num=1, int delim=EOF );</pre> </td> </tr> </table> <p>The ignore() function is used with input streams. It reads and throws away characters until <i>num</i> characters have been read (defaults to 1) or until the character <i>delim</i> is read (which defaults to EOF).</p> <i>Related topics:</i><br> <strong><a href="#get">get()</a>, <a href="#getline">getline()</a></strong> <hr> <h2><a name="open">open</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void open( const char *filename ); void open( const char *filename, openmode mode );</pre> </td> </tr> </table> <p>The function open() is used with file streams. It opens <i>filename</i> and associates it with the current stream. The optional <i>mode</i> can be:</p> <a name="mode_flags"></a> <table> <tr> <th>Mode</th> <th>Meaning</th> </tr> <tr bgcolor="#eeeeff"> <td>ios::app</td> <td>append output</td> </tr> <tr> <td>ios::ate</td> <td>seek to EOF when opened</td> </tr> <tr bgcolor="#eeeeff"> <td>ios::binary</td> <td>open the file in binary mode</td> </tr> <tr> <td>ios::in</td> <td>open the file for reading</td> </tr> <tr bgcolor="#eeeeff"> <td>ios::out</td> <td>open the file for writing</td> </tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -