通过C ++类进行文件处理
在本教程中,我们将讨论一个程序,以了解通过C++类进行的文件处理。
用户可以使用类定义在文件处理中用于与文件交互的默认功能。以下是ifstream和ofstream函数的实现。
示例
#include <iostream>
#include <fstream>
using namespace std;
int main(){
//creating ofstream object ofstream fout;
string line;
fout.open("sample.txt");
//initiating loop if file is opened
while (fout) {
getline(cin, line);
if (line == "-1")
break;
fout << line << endl;
}
fout.close();
ifstream fin;
fin.open("sample.txt");
while (fin) {
getline(fin, line);
cout << line << endl;
}
fin.close();
return 0;
}热门推荐
2 祝福语简短古诗词
10 38节贺卡简短祝福语
11 老师祝福语简短100字
12 六一互换礼物祝福语简短
13 看病贺卡祝福语简短英文
14 对航母的祝福语简短
15 虎年台词祝福语大全简短
16 提早庆祝生日祝福语简短
17 简短大气的狗年祝福语
18 元旦的祝福语简短 公司