当前位置: 首页 > article >正文

C++之文件操作

文件分类


文本文件:以 ASCII码 存储

二进制文件:以 二进制 形式存储

操作文件:


1. ofstream:写操作
2. ifstream: 读操作
3. fstream : 读写操作

写文件


1. 包含头文件   

     #include <fstream>

2. 创建流对象  

   ofstream ofs;

3. 打开文件

   ofs.open("文件路径",打开方式);

4. 写数据

   ofs << "写入的数据";

5. 关闭文件

   ofs.close();

#include<iostream>
using namespace std;
#include<fstream>

int main()
{
	ofstream ofs;
	ofs.open("test.txt", ios::out);
	ofs << "姓名:张三" << endl;
	ofs << "性别:男" << endl;
	ofs << "年龄:18" << endl;
	ofs.close();

	system("pause");
	return 0;
}

读文件


1. 包含头文件   

     #include <fstream>

2. 创建流对象  

   ifstream ifs;

3. 打开文件并判断文件是否打开成功

   ifs.open("文件路径",打开方式);

4. 读数据

   四种方式读取

5. 关闭文件

   ifs.close();

#include<iostream>
using namespace std;
#include <fstream>
#include <string>

void tet()
{
	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;
	}

	//第四种
	char c;
	while ((c = ifs.get()) != EOF)
	{
		cout << c;
	}

	ifs.close();


}

int main() {

	tet();

	system("pause");

	return 0;
}

二进制文件


打开方式要指定为 ios::binary

二进制 写文件 


二进制方式写文件主要利用流对象调用成员函数write

函数原型 :ostream& write(const char * buffer,int len);

字符指针buffer:指向内存中一段存储空间。

len:读写的字节数

#include <fstream>
#include <string>

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

//二进制文件  写文件
void test01()
{
	//1、包含头文件

	//2、创建输出流对象
	ofstream ofs("person.txt", ios::out | ios::binary);
	
	//3、打开文件
	//ofs.open("person.txt", ios::out | ios::binary);

	Person p = {"张三"  , 18};

	//4、写文件
	ofs.write((const char *)&p, sizeof(p));

	//5、关闭文件
	ofs.close();
}

int main() {

	test01();

	system("pause");

	return 0;
}

二进制 读文件


二进制方式读文件主要利用流对象调用成员函数read

函数原型:istream& read(char *buffer,int len);

 

#include <fstream>
#include <string>

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

void test01()
{
	ifstream ifs("person.txt", ios::in | ios::binary);
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;
	}

	Person p;
	ifs.read((char *)&p, sizeof(p));

	cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

http://www.kler.cn/news/319724.html

相关文章:

  • 考虑电网交互及禁止运行区的风电、光伏与火电互补调度运行(MATLAB-Yalmip-Cplex全代码)
  • uniapp webview清理缓存
  • 华为云徐峰:AI赋能应用现代化,加速软件生产力跃升
  • 聚合函数count 和 group by
  • 【linux】进度条
  • 常见服务端口号和中文大全
  • Linux:进程(四)
  • 前端三大框架对比与选择
  • JavaEE——多线程的状态及线程安全问题
  • 机器人/无人车 MPC业务架构
  • 快递物流单号识别API接口代码
  • 黑马智数Day5
  • 【设计模式-组合】
  • 【Git入门】使用 Git 进行项目管理:Word Count 程序开发与托管
  • Redis安全
  • Java语法-类和对象(上)
  • 《开题报告》基于SpringBoot的社区团购系统的设计与实现+学习文档+答辩讲解视频
  • 编译win2k3中tools目录下i386mk.inc文件的作用
  • Java 微服务框架 HP-SOA v1.1.4
  • 【网络】高级IO——Reactor版TCP服务器
  • 刷题训练之栈
  • 系统敏感信息搜索工具(支持Windows、Linux)
  • Unnity IOS安卓启动黑屏加图(底图+Logo gif也行)
  • docker中搭建nacos并将springboot项目的配置文件转移到nacos中
  • 流 Stream
  • 代码随想录算法训练营第二十天 | Java |530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先
  • 聚观早报 | 小米三折叠手机专利曝光;李斌谈合肥投资蔚来
  • K8S服务发布
  • 操作系统 | 学习笔记 | | 王道 | 5.3 磁盘和固态硬盘
  • Qwen大型语言模型系列的最新成果 ----Qwen2.5