本文概述
- 建议:在继续解决方案之前, 请先在{IDE}上尝试使用你的方法。
- C ++
- C ++
流:
流类以写入文件
ifstream:
流类以从文件中读取
fstream:
流类以读取和写入文件。

文章图片
现在第一步是打开特定文件进行读取或写入操作。我们可以通过打开文件
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通过激烈的问题解决过程来训练和掌握这些概念。
推荐阅读
- 亚马逊SDE-1 FTE/6个月实习机会– AmazeWow
- Python合并CSV(如何合并两个或多个CSV文件为一个文件())
- Python如何将数组写入CSV文件((数组转为CSV)三种方法)
- u盘无法格式化怎样办?本文教您处理办法
- 笔记本系统,本文教您笔记本U盘怎样安装win8系统
- u盘打开盘制作软件,本文教您如何制作u盘打开盘
- u盘读不出来,本文教您u盘读不出来如何修好
- u打开盘,本文教您如何迅速制作U盘打开盘
- u盘不能拷文件,本文教您处理u盘不能拷文件