C++的文件读写

文件读写的意义

为什么需要文件读写?答:文件读写的目的是为了永久保存用户数据,否则程序一退出,数据就会消失。

C++方式

IO的含义

IO(input/output):指的是向设备输入数据和输出数据。

而IO设备有:文件、控制台、特定的数据类型stringstream。

C++中的IO流正是向这些IO设备写入数据(输出流、写文件),再从IO设备中读出数据(输入流、读文件)。是的,你没看错,这里是程序向设备输出设备对程序输入,一定要搞清楚对象。

C++利用文件流进行文件读写

其中涉及的类库有:ifstream(对文件进行读操作)、ofstream(对文件进行写操作)、fstream(对文件进行读、写操作)。

这些类库的头文件是:<fstream> 另外如遇报错,请使用命名空间 std。

就像是 string 类,即使你包含了头文件 <string> ,可定义一个变量还是要加上 std:: , 除非你使用了命名空间 std 。

文本文件读写

文件打开的方式

fstream

|-------------|-------------------------------|
| 模式标志 | 描述 |
| ios::in | 读方式打开文件 |
| ios::out | 写方式打开文件 |
| ios::trunc | 如果此文件已经存在,就会在打开文件之前,把文件长度截断为0 |
| ios::app | 尾部追加方式(在尾部后进行写入) |
| ios::ate | 文件打开后,光标定位到文件尾 |
| ios::binary | 二进制方式 |

如果选择多种模式可以用位操作符 | 连接 ,例如:

复制代码
fstream ffile;
ffile.open("test.txt",ios::in | ios::trunc) //以截断和读的方式打开文本文件

因为 fstream 类既能读又能写,我就讲 ifstream 和 ofstream 类。

复制代码
#include <iostream>
#include <fstream>

using namespace std;
//文本文件读写
int main(void)
{
    
    //文件写
	ofstream out; //定义一个文件输出流对象
	string name;
	int age;

	out.open("test.txt"); //默认方式打开文件
	while (1)
	{
		cout << "请输入姓名:[ctrl+z退出]";
		cin >> name;
		if (cin.eof()) //如果是文件结束符,退出循环
		{
			break;
		}
		out << name << " "; //姓名和空格写入文件

		
		cout << "请输入年龄:";
		cin >> age;
		
		out << age << endl; //年龄和回车写入文件
		
	}

	out.close();//关闭打开的文件流

    //文件读
	ifstream in;
	in.open("test.txt");

	while (1)
	{
		in >> name;
		if (in.eof()) //读到文件结束符了
		{
			break;
		}
		cout << name << " ";

		in >> age;
		if (in.eof()) 
		{
			break;
		}
		cout << age << endl;
	}

	in.close();
	return 0;  
}

二进制文件读写

复制代码
#include <iostream>
#include <fstream>

using namespace std;
//二进制方式读写文件
int main(void)
{
    //写二进制文件
	ofstream out;
	string name;
	int age;

	out.open("date.dat", ios::out | ios::binary);
	while (1)
	{
		cout << "请输入姓名:【ctrl+z退出】";
		cin >> name;
		if (cin.eof()) //遇到文件结束符,也就是文件为空了
		{
			break;
		}
		out << name << '\t';


		cout << "请输入年龄:";
		cin >> age;
		if (cin.eof())
		{
			break;
		}

		//out << age << endl; //还是以文本形式写文件
		out.write((char*) & age, sizeof(age)); //注意需要强制转换

	}
	out.close();


	//读二进制文件
	ifstream in;

	in.open("date.dat", ios::in | ios::binary);  // 其中 ios::binary 代表以二进制方式
	
	while (1)
	{
		in >> name;
		if (in.eof())
		{
			break;
		}
		cout << name << '\t';

		/*读取年龄前的字符 '\t' ,否则读取的不正常,因为读取二进制文件时不会跳过空格回车制表符*/
		char tmp;
		in.read((char*)&tmp, sizeof(tmp)); 


		//in >> age ; //错误读取不了二进制文件,会一直死循环
		in.read((char*)&age, sizeof(age));
		cout << age << endl;
		
	}
    in.close();
    return 0;

}

按指定格式读写文本文件

复制代码
//指定格式读写文件
#include <iostream>
#include <string>
#include <fstream>
#include <sstream> // stringstream类的头文件


using namespace std;

int main(void)
{
	//写
	string name;
	int age;

	ofstream outfile;
	outfile.open("test.txt");

	while (1)
	{
		cout << "请输入姓名:[ctrl+z结束]";
		cin >> name;
		if (cin.eof())
		{
			break;
		}

		cout << "请输入年龄:";
		cin >> age;

		stringstream s;
		s << "姓名:" << name << "\t年龄:" << age << endl;

		//把指定格式的数据写入文件
		outfile << s.str(); //转成string 对象


	}
	outfile.close();
	


	//读
	ifstream infile;
	string line;
	char name2[32];
	

	infile.open("test.txt");

	while (1)
	{
		getline(infile, line);  //从文件中读取一行,保存在line中

		if (infile.eof())
		{
			break;
		}

		sscanf_s(line.c_str(), "姓名:%s 年龄:%d", name2, sizeof(name2), &age); //C语言的方式
		cout << "姓名:" << name2 << "	年龄:" << age << endl;
	}

	infile.close();
	return 0;

}
相关推荐
daiwoliyunshang6 分钟前
哈希表实现(1):
数据结构·c++
pystraf11 分钟前
模板分享:网络最小费用流
c++·算法·图论·网络流
thisiszdy27 分钟前
<C++> MFC自动关闭对话框(MessageBoxTimeout)
c++·mfc
绯樱殇雪37 分钟前
编程题 03-树2 List Leaves【PAT】
c++·pat考试
✿ ༺ ོIT技术༻1 小时前
笔试强训:Day5
c++·算法
努力的小帅1 小时前
C++_STL_map与set
开发语言·数据结构·c++·学习·leetcode·刷题
双叶8362 小时前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)
c语言·开发语言·数据结构·c++·windows
末央&2 小时前
【数据结构】手撕AVL树(万字详解)
数据结构·c++
序属秋秋秋2 小时前
《数据结构初阶》【二叉树 精选9道OJ练习】
c语言·数据结构·c++·算法·leetcode
uyeonashi4 小时前
【Boost搜索引擎】构建Boost站内搜索引擎实践
开发语言·c++·搜索引擎