通过 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;
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP