文件操作(C++)
在头文件 fstream 中定义了三个类型来支持文件的IO:ifstream 从一个给定的文件中读取数据;ofstream 向一个指定的文件中写入数据;fstream 可以读写指定的文件
1.文件模式
每一个文件流都有关联的文件模式(file mode),用来定义如何对文件进行操作。
ios::in 以读的方式打开
ios::out 以写的方式打开
ios::app 每次写的时候都定位到文件的末尾,追加写
ios::ate 打开文件之后立即定位到文件的末尾
ios::trunc 截断文件
ios::binary 以二进制的方式进行读写文件
注意点:
1.只能对 ofstream 或者 fstream 设置为 out 模式
2.只能对 ifstream 或者 fstream 设置为 in 模式
3.只有 out 被设定时,trunc 才能同时被设定
4.out 和 app 不能同时被设定
5.默认情况下,即使没有指定trunc,只要是以out模式打开的文件也会被截断
6.ate 和 binary 模式可以用于任何类型的文件流对象,并且还可以与其他任何模式一起使用
2.示例代码
2.1 向指定文件写入内容
#include<iostream>
#include<fstream>
using namespace std;
void test() {
//向指定文件写入内容
ofstream ofs;
//如果没有test.txt文件,则会先创建一个文件,文件位置在和原文件的同级目录下
ofs.open("test.txt", ios::out);
//向文件写入指定内容
ofs << "感觉不如原神...画质" << endl;
ofs << "123456" << endl;
ofs << "123456" << endl;
//关闭文件
ofs.close();
}
int main() {
test();
system("pause");
return 0;
}
文件位置
文件内容
如果我们修改了想要写入的内容再次运行
ofs << "你说的对,但原神是一款..." << endl;
ofs << "你说的对,但原神是一款..." << endl;
ofs << "你说的对,但原神是一款..." << endl;
文件内容
可以得出结论:out模式 默认是trunc写入的,也就是每次打开文件,都会先清除里面内容,再写入。
我们如果想使用追加的方式,可以添加app模式
ofstream ofs;
ofs.open("test.txt", ios::out | ios::app);
ofs << "你的素养很差..." << endl;
ofs << "你的素养很差..." << endl;
ofs << "你的素养很差..." << endl;
ofs << "你的素养很差..." << endl;
ofs.close();
再次执行之后的结果为
2.2 读取指定文件的内容
1.第一种读取方式
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void test() {
ifstream ifs;
ifs.open("test.txt", ios::in);
//判断是否打开失败,如果失败直接返回
if (!ifs.is_open()) {
cout << "打开文件失败" << endl;
return;
}
//从文件读入数据
char buf[1024] = { 0 };
while (ifs >> buf) {
cout << buf << endl;
}
ifs.close();
}
int main() {
test();
system("pause");
return 0;
}
读入结果
第一种方式的缺点是遇到空格会直接跳过
如果txt文件的内容修改一下
则会得到这样的结果
2.第二种读入方式
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void test() {
ifstream ifs;
ifs.open("test.txt", ios::in);
if (!ifs.is_open()) {
cout << "打开文件失败" << endl;
return;
}
//char buf[1024] = { 0 };
//while (ifs >> buf) {
// cout << buf << endl;
//}
//第二种读入方式
char buf[1024] = { 0 };
while (ifs.getline(buf, sizeof buf)) {
cout << buf << endl;
}
ifs.close();
}
int main() {
test();
system("pause");
return 0;
}
读入的结果为
第二种方式是整行读入,所以空格也会正常读入
3.第三种读入方式
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void test() {
ifstream ifs;
ifs.open("test.txt", ios::in);
if (!ifs.is_open()) {
cout << "打开文件失败" << endl;
return;
}
//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;
}
ifs.close();
}
int main() {
test();
system("pause");
return 0;
}
读入的结果为
第三种读入方式也是整行的读入,但是使用的是字符串。
2.3 以二进制的方式写入数据
#include<iostream>
#include<fstream>
using namespace std;
class Person {
public:
char name[64];
int age;
};
void test() {
ofstream ofs("person.txt", ios::out | ios::binary);
//创建一个Person对象,再写入到person.txt中
Person p = { "感觉画质不如原神",20 };
ofs.write((const char*)&p, sizeof p);
ofs.close();
}
int main() {
test();
system("pause");
return 0;
}
写入结果为
因为是以二进制的方式读入的,只有计算机能识别里面的内容
2.4 以二进制的方式读入数据
#include<iostream>
#include<fstream>
using namespace std;
class Person {
public:
char name[64];
int age;
};
void test() {
ifstream ifs("person.txt", ios::in | ios::binary);
if (!ifs.is_open()) {
cout << "文件读取失败,自动退出" << endl;
}
Person p;
ifs.read((char*)&p, sizeof Person);
cout << "name : " << p.name << " age: " << p.age << endl;
ifs.close();
}
int main() {
test();
system("pause");
return 0;
}
读入结果为
二进制读入读出的过程就是序列化与反序列化