操作文件的三大类
C++ 标准库中还专门提供了 3 个类用于实现文件操作,它们统称为文件流类,这 3 个类分别为:
- ifstream:专用于从文件中读取数据;
- ofstream:专用于向文件中写入数据;
- fstream:既可用于从文件中读取数据,又可用于向文件中写入数据。
这 3 个文件流类都位于 <fstream> 头文件中,因此在使用它们之前,程序中应先引入此头文件。
这 3 个文件流类的继承关系:
fstream类常用成员方法:
文件类型
文本文件:文件以文本的ASCII码形式存储在计算机
二进制文件:文件以文本的二进制形式存储在计算机中
打开文件
在对文件进行读写操作之前,先要打开文件。可以通过以下两种方式打开文件:
- 调用流对象的 open 成员函数打开文件。
- 定义文件流对象时,通过构造函数打开文件。
使用open函数打开文件:
void open(const char* szFileName, int mode)
-
第一个参数是指向文件名的指针,
-
第二个参数是文件的打开模式标记,文件的打开模式标记代表了文件的使用方式,这些标记可以单独使用,也可以利用|操作符组合使用。
下标列出各种模式标记单独使用时的作用,以及常见的两种模式标记组合的作用:
| 模式标记 | 适用对象 | 作用 |
|---|---|---|
| ios::in | ifstream fstream | 打开文件用于读取数据。如果文件不存在,则打开出错。 |
| ios::out | ofstream fstream | 打开文件用于写入数据。如果文件不存在,则新建该文件;如果文件原来就存在,则打开时清除原来的内容。 |
| ios::app | ofstream fstream | 打开文件,用于在其尾部添加数据。如果文件不存在,则新建该文件。 |
| ios::ate | ifstream | 打开一个已有的文件,并将文件读指针指向文件末尾(读写指 的概念后面解释)。如果文件不存在,则打开出错。 |
| ios:: trunc | ofstream | 打开文件时会清空内部存储的所有数据,单独使用时与 ios::out 相同。 |
| ios::binary | ifstream ofstream fstream | 以二进制方式打开文件。若不指定此模式,则以文本模式打开。 |
| ios::in | ios::out | fstream | 打开已存在的文件,既可读取其内容,也可向其写入数据。文件刚打开时,原有内容保持不变。如果文件不存在,则打开出错。 |
| ios::in | ios::out | ofstream | 打开已存在的文件,可以向其写入数据。文件刚打开时,原有内容保持不变。如果文件不存在,则打开出错。 |
| ios::in | ios::out | ios::trunc | fstream | 打开文件,既可读取其内容,也可向其写入数据。如果文件本来就存在,则打开时清除原来的内容;如果文件不存在,则新建该文件。 |
1、文本文件
写文件
//包含头文件
#include<fstream>
using namespace std;
#include<iostream>
void test()
{
//创建流对象
ofstream ofs;
//打开文件
ofs.open("test.txt", ios::out);
//写入文件内容
ofs << "姓名:张三" << endl;
ofs << "年龄:118" << endl;
ofs << "性别:男" << endl;
//关闭流
ofs.close();
}
int main()
{
test();
system("pause");
return 0;
}
读文件
#include<fstream>
using namespace std;
# include<string>
#include<iostream>
void test()
{
ifstream ifs;
ifs.open("test.txt", ios::in);
if (!ifs)
{
cout << "文件打开出错" << endl;
return;
}
//4.读数据
//第一种
char buf[1024] = {0};
while (ifs >> buf) {
cout << buf << endl;
}
//第二种
char buf[1024] = {0};
while (ifs.getline(buf, sizeof(buf))) {
cout << buf << endl;
}
//第三种
string buf;
while( getline(ifs, buf) ) {
cout << buf << endl;
}
//第四种
char c;
while ((c = ifs.get()) != EOF){
cout << c;
}
ifs.close();
}
int main()
{
test();
system("pause");
return 0;
}
2、二进制文件
写文件
将内存中 buffer 指向的 count 个字节的内容写入文件
ostream & write(char* buffer, int count);
-
buffer 用于指定要写入文件的二进制数据的起始位置;
-
count 用于指定写入字节的个数。
#include<fstream>
using namespace std;
# include<string>
#include<iostream>
class Person
{
public:
char m_name[64];
int m_age;
};
void test()
{
ofstream ofs;
//以二进制文本模式打开文件
ofs.open("person.txt", ios::out | ios::binary);
Person p = {"张三",18};
ofs.write((const char*)&p, sizeof(p));
ofs.close();
}
int main()
{
test();
system("pause");
return 0;
}
读文件
从文件中读取 count 个字节的数据
istream & read(char* buffer, int count);
- buffer 用于指定读取字节的起始位置,
- count 指定读取字节的个数
#include<fstream>
using namespace std;
#include<iostream>
class Person
{
public:
char m_name[64];
int m_age;
};
void test()
{
ifstream ifs;
ifs.open("person.txt", ios::out | ios::binary);
if (!ifs)
{
cout << "文件读取失败" <<endl ;
return;
}
Person p;
ifs.read((char*)&p, sizeof(p));
cout << "p的姓名" << p.m_name << "p的年龄:" << p.m_age << endl;
ifs.close();
}
int main()
{
test();
system("pause");
return 0;
}