如何使用C++类进行文件处理(代码示例)

本文概述

  • 建议:在继续解决方案之前, 请先在{IDE}上尝试使用你的方法。
  • C ++
  • C ++
在C ++中, 文件主要通过使用fstream头文件中可用的三个类fstream, ifstream, ofstream来处理。
流:
流类以写入文件
ifstream:
流类以从文件中读取
fstream:
流类以读取和写入文件。
如何使用C++类进行文件处理(代码示例)

文章图片
现在第一步是打开特定文件进行读取或写入操作。我们可以通过打开文件
1.创建对象时在构造函数中传递文件名
2.使用open方法
例如
使用构造函数ifstream打开文件(const char *文件名, ios_base :: openmode模式= ios_base :: in); ifstream fin(filename, openmode)默认情况下openmode = ios :: in ifstream fin(" filename"); 使用打开方法打开文件调用默认构造函数ifstream fin; fin.open(文件名, openmode)fin.open("文件名");
模式:
会员常数 代表 访问
在* 输入 打开文件以供读取:内部流缓冲区支持输入操作。
输出如下 打开文件进行写入:内部流缓冲区支持输出操作。
二元 二元 操作以二进制模式而非文本执行。
吃了 最后 输出位置从文件末尾开始。
应用程式 附加 所有输出操作都发生在文件末尾, 并附加到其现有内容之后。
截断 截短 打开之前文件中存在的所有内容都将被丢弃。
默认打开模式:
ifstream ios :: in
ios :: out
ios :: in | ios :: out
问题陈述
:使用C ++读写文件。
例子:
Input : Welcome in lsbin. Best way to learn things. -1 Output : Welcome in lsbin. Best way to learn things.

推荐:请尝试以下方法{IDE}首先, 在继续解决方案之前。 下面是通过使用实现
ifsream和ofstream类
.
C ++
/* File Handling with C++ using ifstream & ofstream class object*/ /* To write the Content in File*/ /* Then to read the content of file*/ #include < iostream> /* fstream header file for ifstream, ofstream, fstream classes */ #include < fstream> using namespace std; // Driver Code int main() { // Creation of ofstream class object ofstream fout; string line; // by default ios::out mode, automatically deletes // the content of file. To append the content, open in ios:app // fout.open("sample.txt", ios::app) fout.open( "sample.txt" ); // Execute a loop If file successfully opened while (fout) {// Read a Line from standard input getline(cin, line); // Press -1 to exit if (line == "-1" ) break ; // Write line in file fout < < line < < endl; }// Close the File fout.close(); // Creation of ifstream class object to read the file ifstream fin; // by default open mode = ios::in mode fin.open( "sample.txt" ); // Execute a loop until EOF (End of File) while (fin) {// Read a Line from File getline(fin, line); // Print line in Console cout < < line < < endl; }// Close the file fin.close(); return 0; }

下面是通过使用实现
fstream类
.
C ++
/* File Handling with C++ using fstream class object */ /* To write the Content in File */ /* Then to read the content of file*/ #include < iostream> /* fstream header file for ifstream, ofstream, fstream classes */ #include < fstream> using namespace std; // Driver Code int main() { // Creation of fstream class object fstream fio; string line; // by default openmode = ios::in|ios::out mode // Automatically overwrites the content of file, To append // the content, open in ios:app // fio.open("sample.txt", ios::in|ios::out|ios::app) // ios::trunc mode delete all conetent before open fio.open( "sample.txt" , ios::trunc | ios::out | ios::in); // Execute a loop If file successfully Opened while (fio) {// Read a Line from standard input getline(cin, line); // Press -1 to exit if (line == "-1" ) break ; // Write line in file fio < < line < < endl; }// Execute a loop untill EOF (End of File) // point read pointer at beginning of file fio.seekg(0, ios::beg); while (fio) {// Read a Line from File getline(fio, line); // Print line in Console cout < < line < < endl; }// Close the file fio.close(); return 0; }

【如何使用C++类进行文件处理(代码示例)】被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。

    推荐阅读