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;

}
相关推荐
周哈里窗的编程2 小时前
CSP-CCF★201912-2回收站选址★
c++·算法·图论
未来可期LJ4 小时前
【C++ 设计模式】单例模式的两种懒汉式和饿汉式
c++·单例模式·设计模式
Trouvaille ~5 小时前
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
c++·c++20·编译原理·编译器·类和对象·rvo·nrvo
little redcap5 小时前
第十九次CCF计算机软件能力认证-乔乔和牛牛逛超市
数据结构·c++·算法
机器视觉知识推荐、就业指导5 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
孤寂大仙v6 小时前
【C++】STL----list常见用法
开发语言·c++·list
咩咩大主教6 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
Ylucius8 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
是店小二呀8 小时前
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
开发语言·c++·后端
ephemerals__9 小时前
【c++】动态内存管理
开发语言·c++