📄 all.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>C++ I/O</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ I/O</a> </div> <div class="name-format"> bad </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> bool bad();</pre> <p>The bad() function returns true if a fatal error with the current stream has occurred, false otherwise.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="eof.html">eof</a><br> <a href="fail.html">fail</a><br> <a href="good.html">good</a><br> <a href="rdstate.html">rdstate</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> clear </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> void clear( iostate flags = ios::goodbit );</pre> <p>The function clear() does two things:</p> <ul> <li>it clears all <a href="../io_flags.html#state_flags">io stream state flag</a>s associated with the current stream,</li> <li>and sets the flags denoted by <em>flags</em></li> </ul> <p>The <em>flags</em> argument defaults to ios::goodbit, which means that by default, all flags will be cleared and ios::goodbit will be set.</p> <div class="related-examples-format"> Example code: </div> <div class="related-examples"> <p>For example, the following code uses the clear() function to reset the flags of an output file stream, after an attempt is made to read from that output stream:</p> <pre class="example-code"> fstream outputFile( "output.txt", fstream::out ); // try to read from the output stream; this shouldn't work int val; outputFile >> val; if( outputFile.fail() ) { cout << "Error reading from the output stream" << endl; // reset the flags associated with the stream outputFile.clear(); } for( int i = 0; i < 10; i++ ) { outputFile << i << " "; } outputFile << endl; </pre> </div> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="eof.html">eof</a><br> <a href="fail.html">fail</a><br> <a href="good.html">good</a><br> <a href="rdstate.html">rdstate</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> close </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> void close();</pre> <p>The close() function closes the associated file stream.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="constructors.html">I/O Constructors</a><br> <a href="open.html">open</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> I/O Constructors </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> fstream( const char *filename, openmode mode ); ifstream( const char *filename, openmode mode ); ofstream( const char *filename, openmode mode );</pre> <p>The fstream, ifstream, and ofstream objects are used to do file I/O. The optional <em>mode</em> defines how the file is to be opened, according to the <a href="../io_flags.html#mode_flags">io stream mode flag</a>s. The optional <em>filename</em> specifies the file to be opened and associated with the stream.</p> <p>Input and output file streams can be used in a similar manner to C++ predefined I/O streams, cin and cout.</p> <div class="related-examples-format"> Example code: </div> <div class="related-examples"> <p>The following code reads input data and appends the result to an output file.</p> <pre class="example-code"> ifstream fin( "/tmp/data.txt" ); ofstream fout( "/tmp/results.txt", ios::app ); while( fin >> temp ) fout << temp + 2 << endl; fin.close(); fout.close(); </pre> </div> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="close.html">close</a><br> <a href="open.html">open</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> eof </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> bool eof();</pre> <p>The function eof() returns true if the end of the associated input file has been reached, false otherwise.</p> <p>For example, the following code reads data from an input stream <em>in</em> and writes it to an output stream <em>out</em>, using eof() at the end to check if an error occurred:</p> <pre class="example-code"> char buf[BUFSIZE]; do { in.read( buf, BUFSIZE ); std::streamsize n = in.gcount(); out.write( buf, n ); } while( in.good() ); if( in.bad() || !in.eof() ) { // fatal error occurred } in.close(); </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="bad.html">bad</a><br> <a href="clear.html">clear</a><br> <a href="fail.html">fail</a><br> <a href="good.html">good</a><br> <a href="rdstate.html">rdstate</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> C++ I/O Examples </div> <h2>Reading From Files</h2> <p>Assume that we have a file named <em>data.txt</em> that contains this text:</p> <pre class="example-data"> Fry: One Jillion dollars. [Everyone gasps.] Auctioneer: Sir, that's not a number. [Everyone gasps.] </pre> <p>We could use this code to read data from the file, word by word:</p> <pre class="example-code"> ifstream fin("data.txt"); string s; while( fin >> s ) { cout << "Read from file: " << s << endl; }</pre> <p>When used in this manner, we'll get space-delimited bits of text from the file:</p> <pre class="example-output"> Read from file: Fry: Read from file: One Read from file: Jillion Read from file: dollars. Read from file: [Everyone Read from file: gasps.] Read from file: Auctioneer: Read from file: Sir, Read from file: that's Read from file: not Read from file: a Read from file: number. Read from file: [Everyone Read from file: gasps.]</pre> <p>Note that in the previous example, all of the whitespace that separated words (including newlines) was lost. If we were interested in preserving whitespace, we could read the file in line-by-line using the <a href="getline.html">I/O getline() function</a>.</p> <pre class="example-code"> ifstream fin("data.txt"); const int LINE_LENGTH = 100; char str[LINE_LENGTH]; while( fin.getline(str,LINE_LENGTH) ) { cout << "Read from file: " << str << endl; }</pre> <p>Reading line-by-line produces the following output:</p> <pre class="example-output"> Read from file: Fry: One Jillion dollars. Read from file: [Everyone gasps.] Read from file: Auctioneer: Sir, that's not a number. Read from file: [Everyone gasps.]</pre> <p>If you want to avoid reading into character arrays, you can use the <a href="../cppstring/getline.html">C++ string getline()</a> function to read lines into <a href="../cppstring/index.html">strings</a>:</p> <pre class="example-code"> ifstream fin("data.txt"); string s; while( getline(fin,s) ) { cout << "Read from file: " << s << endl; }</pre> <h2>Checking For Errors</h2> <p>Simply evaluating an I/O object in a boolean context will return false if any errors have occurred:</p> <pre class="example-code"> string filename = "data.txt"; ifstream fin( filename.c_str() ); if( !fin ) { cout << "Error opening " << filename << " for input" << endl; exit(-1); }</pre> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> fail </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> bool fail();</pre> <p>The fail() function returns true if an error has occurred with the current stream, false otherwise.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="bad.html">bad</a><br> <a href="clear.html">clear</a><br> <a href="eof.html">eof</a><br> <a href="good.html">good</a><br> <a href="rdstate.html">rdstate</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> fill </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> char fill(); char fill( char ch );</pre> <p>The function fill() either returns the current fill character, or sets the current fill character to <em>ch</em>.</p> <p>The fill character is defined as the character that is used for padding when a number is smaller than the specified <a href= "width.html">width</a>(). The default fill character is the space character.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="precision.html">precision</a><br> <a href="width.html">width</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> flags </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <fstream> fmtflags flags(); fmtflags flags( fmtflags f );</pre> <p>The flags() function either returns the <a href= "../io_flags.html#format_flags">io stream format flag</a>s for the current stream, or sets the flags for the current stream to be <em>f</em>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -