int get();
istream& get(char& c);
EOF。EOF 时,第 24?29 行的循环终止。
// This program demonstrates the use of the get member
// functions of the istream class
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//Variables needed to read file one character at a time
string fileName;
fstream file;
char ch; // character read from the file
// Get file name and open file
cout << "Enter a file name: ";
cin >> fileName;
file.open(fileName, ios::in);
if (!file)
{
cout << fileName << " could not be opened .\n";
return 0;
}
// Read file one character at a time and echo to screen
ch = file.get ();
while (ch != EOF)
{
cout << ch;
ch = file.get();
}
// Close file
file.close ();
return 0;
}
此程序将显示任何文件的内容。由于 get 函数不会跳过白色空格,因此所有字符都将按照文件中的出现方式显示。
file.get(ch);
while (!file.fail ())
{
cout << ch;
file.get(ch);
}
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有