一、fstream
文件输入输出流
1、文件读写的三种类型
- ifstream:从一个给定文件中读取数据 。
- ofstream:向一个给定的文件中写入数据
- fstream:可以读写给定的文件。
它可以用IO库中的<<
、>>
、getline()
等方式来读写文件。其中getline()只能用来读取字符串,不可以读取数值类型。
2、fstream
的一些操作
当一个 fstream 对象被销毁时,close
函数会被自动调用。
// 创建一个未绑定的文件流
fstream ftm01;
// 创建一个ftm02对象,并打开名为str的文件
fstream ftm02(str);
// 创建一个ftm03对象,并以mode模式打开名为str的文件
fstream ftm03(str, mode);
// 打开名为str的文件,并将该文件与ftm01绑定
ftm01.open(str);
// 关闭与ftm01绑定的文件,返回void
ftm01.close();
// 表示与ftm01关联的文件是否成功打开且尚未关闭
ftm01.is_open();
3、文件模式
(1)、每个流都有一个关联的文件模式,用来指出如何使用文件。如下所示:
in
:以读的方式打开out
:以写的方式打开app
:每次写操作前定位到文件末尾,接着后面写入内容,避免覆盖之前的内容ate
:打开文件后立即定位到文件末尾trunc
:截断文件binary
:以二进制方式进行IO
(2)、无论用哪种方式来打开文件,都可以指定文件的模式。
- 只可以对 ifstream 或 fstream 对象设定
in
模式 - 只可以对 ofstream 或 fstream 对象设定
out
模式 - 只用当
out
模式被设定时,才可以设定trunc
模式 - 只要
trunc
模式没有被设定,就可以设定app
模式。在app
模式下即使没有显式指定out
模式,文件也总是以输出方式打开。 - 默认情况下,即使没有指定
trunc
模式,以out
模式打开的文件也会被截断。为了保留以out
模式打开的文件内容,必须同时指定app
模式,这样会将数据追加到文件的末尾。或者同时指定in
模式,即打开文件同时进行读写操作。 ate
和binary
模式可以用于任何类型的文件流对象,且可以与其他任何文件模式组合使用。
(3)、每个文件流类型都定义了一个默认的文件模式,当未指定文件模式时,就会采用默认的文件模式。
- 与 ifstream 关联的文件默认以
in
模式打开,即读取文件中内容 - 与 ofstream 关联的文件默认以
out
模式打开,即向文件中写入新内容 - 与 fstream 关联的文件默认以
in
和out
模式打开,可读可写
(4)、以out
模式打开文件并向其中写入内容时,会默认丢弃原有数据
默认情况下,当以 ofstream 打开一个文件并写入内容时,文件的内容会被清空。阻止文件内容被清空的方法是指定 app
模式。保留被 ofstream 打开文件中已有数据的唯一方法是显式指定app
或in
模式。
// 以默认方式打开文件,文件内容被清空
ofstream ofm01("fileName");
// 以out模式打开文件时,默认trunc,文件内容被清空
ofstream ofm02("fileName", ofstream::out);
// 文件内容被清空
ofstream ofm03("fileName", ofstream::out | ofstream::trunc);
// 以app 模式打开文件,保留文件原有内容
ofstream ofm04("fileName", ofstream::app);
ofstream ofm05("fileName", ofstream::out | ofstream::app);
4、常用流状态成员函数
eof()
: 表示如果读文件到达文件末尾,返回truebad()
:如果在读写过程中出错,返回 truefail()
:读写过程出错或格式错误时返回true
二、ifstream
读取文件
1、使用文件流对象
(1)、当需要读取一个文件时,可以定义一个文件流对象,并将对象与文件关联起来。创建文件流对象时,可以提供文件名,让其与文件流对象进行绑定。若提供给了文件流对象,则open
函数将会被自动调用。
(2)、
2、读取字符文件实例
对下图的文件用不同的方式来读取其内容:
(1)、getline() 方式:该方法会将空格也读取进来,且只能用于提取字符串的输入
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string fileName01 = "D:\\Files\\TheFile\\log01.txt";
ifstream ifm(fileName01); //创建文件流对象并与指定文件绑定
string tmp;
if (ifm.is_open()) // 判断文件是否正常打开
{
while (getline(ifm, tmp)) // 每次读取一行到tmp中,默认以换行符作为分割条件
{
cout << tmp << endl;
}
}
else
cout << "Cant open the file" << endl;
system("pause");
return 0;
}
(2)、>> 方式,该方式会忽略所有的空格。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string fileName01 = "D:\\Files\\TheFile\\log01.txt";
ifstream ifm(fileName01); //创建文件流对象并与指定文件绑定
string tmp;
if (ifm.is_open()) // 判断文件是否正常打开
{
// >>会以空格作为分隔符,读取内容时忽略所有空格
while (ifm>>tmp)
{
cout << tmp << endl;
}
}
else
cout << "Cant open the file" << endl;
system("pause");
return 0;
}
3、读取整数文件实例
如读取下图内容
可以使用eof()
函数或者>>
来作为判断条件。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string fileName01 = "D:\\Files\\TheFile\\log02.txt";
ifstream ifm(fileName01); //创建文件流对象并与指定文件绑定
int tmp;
if (ifm.is_open())
{
while (!ifm.eof()) //判断是否到文件末尾。或用 while(ifm>>tmp)
{
ifm >> tmp;
cout << tmp << " ";
}
}
else
cout << "Cant open the file" << endl;
system("pause");
return 0;
}
三、ofstream
将数据写如文件
1、向一个空文件中写入数据
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string fileName02 = "D:\\Files\\TheFile\\log04.txt";
ofstream ofm02(fileName02); //默认以截断的形式打开文件,且会覆盖原有内容
if (ofm02.is_open())
{
ofm02 << "hello this is just a test!\n";
for (int i = 0; i < 10; ++i)
{
ofm02 << i << " ";
}
ofm02 << endl;
}
else
cout << "The file is not opened!";
ofm02.close();
system("pause");
return 0;
}
2、将一个文件的内容写入到另一个文件中
要读取的数据:
数据即将写入的指定文件中原有的内容:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string fileName01 = "D:\\Files\\TheFile\\log03.txt";
string fileName02 = "D:\\Files\\TheFile\\log04.txt";
ifstream ifm(fileName01);
ofstream ofm(fileName02,ofstream::app);
string str;
if (ofm.is_open() && ifm.is_open())
{
while (getline(ifm, str))
{
ofm << str << endl;
}
}
else
cout << "The file is not opened!";
ofm.close();
ifm.close();
system("pause");
return 0;
}
写入数据后的结果如下
参考资料:
[1] C++文件读写详解(ofstream,ifstream,fstream)
[2] C++对文件进行读写操作
[3] c++文件读写操作