#include <iostream> #include <fstream> using namespace std; int main () { char c, str[256]; ifstream is; cout << "Enter the name of an existing text file: "; cin.get (str,256); is.open (str); // open file while (is.good()) // loop while extraction from file is possible { c = is.get(); // get character from file // if (is.good())//==============当被注释掉会多输出两个空格。但存在时不输出最后的两个空格。 cout << c; } is.close(); // close file return 0; } /************************************************************************/ /* infile.good() ios::good public member functionbool good ( ) const; Check if the state of the stream is good for i/o operations. The function returns true if none of the stream's error flags (eofbit, failbit and badbit) are set. Notice that this function is not the exact opposite of bad(), which only checks whether the badbit error flag is set. The error flags can be checked independently by using any of the member functions eof, fail and bad. The value returned by this function is the same as the result of evaluating: (rdstate() == 0) */ /************************************************************************/