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;

}

相关推荐

  1. c++文件

    2023-12-07 08:34:05       36 阅读
  2. C++文件

    2023-12-07 08:34:05       48 阅读
  3. [C#]文件-1

    2023-12-07 08:34:05       38 阅读
  4. C++二进制文件

    2023-12-07 08:34:05       8 阅读
  5. C++BMP文件

    2023-12-07 08:34:05       18 阅读
  6. C语言】文件

    2023-12-07 08:34:05       25 阅读
  7. C++二进制文件

    2023-12-07 08:34:05       12 阅读
  8. 1.基于C#Dbf文件结构概述)

    2023-12-07 08:34:05       31 阅读
  9. C++利用缓冲区来提高文件效率

    2023-12-07 08:34:05       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-07 08:34:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-07 08:34:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-07 08:34:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-07 08:34:05       20 阅读

热门阅读

  1. C++11改进观察者模式

    2023-12-07 08:34:05       33 阅读
  2. [leetcode 差分数组] 拼车 M

    2023-12-07 08:34:05       35 阅读
  3. ElasticSearch 查询优化手段有哪些?

    2023-12-07 08:34:05       31 阅读
  4. 持续集成部署-k8s-高级调度-亲和力

    2023-12-07 08:34:05       28 阅读
  5. 图论|841钥匙和房间

    2023-12-07 08:34:05       41 阅读
  6. 安全众测-内网渗透常用的工具和命令

    2023-12-07 08:34:05       41 阅读
  7. Android 消息分发机制解读

    2023-12-07 08:34:05       28 阅读
  8. html css样式选择器介绍

    2023-12-07 08:34:05       39 阅读
  9. vue 学习 -- day39(reactive 对比 ref)

    2023-12-07 08:34:05       36 阅读
  10. 一个小问题

    2023-12-07 08:34:05       37 阅读
  11. CSS 如何居中 DIV

    2023-12-07 08:34:05       41 阅读