C++文件操作(文本文件的读写+二进制文件的读写)

原文地址http://www.javayihao.top/detail/168

一:概述

1.程序在运行中产生的数据都是临时数据,程序一旦运行结束会被释放,可以通过文件相关的操作将数据持久保存。

2.文件类型有文本文件(文件以文本的ASCLL码形式存储在计算机中)和二进制文件(文件以文本的二进制形式存储在计算机中,用户一般直接读不懂他们)。

3.C++中对文件操作需要包含头文件<fstream>,涉及到的三大文件操作类:ofstream写操作类;ifstream读操作类;fstream读写操作类。

二:文本文件读写操作

写文件

第1步:包含头文件#include<fstream>

第2步:创建流对象ofstream ofs

第3步:打开文件ofs.open("文件路径",打开方式)

第4步:写文件ofs<<"写入数据"

第5步:关闭文件ofs.close()

注意:

1.文件的打开方式

2.打开方式可以通过|操作符配合使用,如打开二进制写文件 ios::binary|ios:out

3.创建流对象ofstream也可以选择fstream读写类

案例代码

#include <iostream>
using namespace std;
//第1步
#include <fstream>
int main() {	
	//第2步
	ofstream ofs;
	//第3步 当前同级目录生成test文件
	ofs.open("test.txt",ios::app);
	//第4步
	ofs << "helloword";
	//第5步
	ofs.close();
}

读文件

第1步:包含头文件#include<fstream>

第2步:创建流对象ifstream ifs

第3步:打开文件ifs.open("文件路径",打开方式)判断是否打开成功,打开方式和上面写文件一样

第4步:读数据

第5步:关闭文件ifs.close()

案例代码

#include <iostream>
using namespace std;
//第1步
#include <fstream>
void test() {
	//第2步
	ifstream ifs;
	//第3步 
	ifs.open("test.txt", ios::in);
	if (!ifs.is_open()) {
		cout << "文件打开失败";
		return;
	}
	//第4步
	char buf[1024] = { 0 };//字符数组初始化为0
	while (ifs >> buf) {//循环全部读完
		cout << buf << endl;
	}
	//第5步
	ifs.close();
}
int main() {	
	test();
}

注意:第4步读数据方式有四种。其他三种如下

//第二种
char buf[1024] = { 0 };
while (ifs.getline(buf,sizeof(buf))){//一行一行读取
	cout << buf << endl;
}
//第三种
#include <iostream>
#include <string>
using namespace std;
//第1步
#include <fstream>
void test() {
	//第2步
	ifstream ifs;
	//第3步 
	ifs.open("test.txt", ios::in);
	if (!ifs.is_open()) {
		cout << "文件打开失败";
		return;
	}
	//第4步
	string buf;
	while (getline(ifs,buf)){//使用string中的全局函数getline一行行读取,需要导入string头文件
		cout << buf << endl;
	}
	//第5步
	ifs.close();
}
int main() {	
	test();
}
//第四种
#include <iostream>
using namespace std;
//第1步
#include <fstream>
void test() {
	//第2步
	ifstream ifs;
	//第3步 
	ifs.open("test.txt", ios::in);
	if (!ifs.is_open()) {
		cout << "文件打开失败";
		return;
	}
	//第4步
	char c;
	while ((c=ifs.get())!=EOF){//一个一个字符读取、效率不高
		cout << c;
	}
	//第5步
	ifs.close();
}
int main() {	
	test();
}

三:二进制文件读写操作

1.二进制方式对文件操作,打开方式指定为ios::binary,二进制文件不仅可以对内置数据类型的数据操作,还可以对对象类型操作。

2.写入的二进制文件出现乱码不用处理,只要读到的正确即可

写文件

#include <iostream>
using namespace std;
//第1步 头文件                    
#include <fstream>
class person {
public:
	char name[54];//涉及到字符串读写,不要使用c++的string,而是用c的char
	int age;
};
void test() {
	//第2步 流对象
	ofstream ofs;
	//第3步 打开文件
	ofs.open("test.txt", ios::out|ios::binary);
	//第4步写文件
	person p = { "李四",12 };
	ofs.write((const char *)&p,sizeof(p));
	//第5步关闭
	ofs.close();
}
int main() {	
	test();
}

读文件

#include <iostream>
using namespace std;
//第1步
#include <fstream>
class person {
public:
	char name[54];
	int age;
};
void test() {
	//第2步
	ifstream ifs;
	//第3步 
	ifs.open("test.txt", ios::in|ios::binary);
	if (!ifs.is_open()) {
		cout << "文件读取失败";
		return;
	}
	//第4步
	person p ;
	ifs.read((char *)&p,sizeof(p));
	cout << p.name << p.age;
	//第5步
	ifs.close();
}
int main() {	
	test();
}

发布了260 篇原创文章 · 获赞 112 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/qq_34491508/article/details/103381514