C++核心高级编程 --- 5.文件操作

文章目录

    • 第五章:
      • 5.文件操作
        • [5.1 文本文件](#5.1 文本文件)
          • [5.1.1 写文件](#5.1.1 写文件)
          • [5.1.2 读文件](#5.1.2 读文件)
        • [5.2 二进制文件](#5.2 二进制文件)
          • [5.2.1 写文件](#5.2.1 写文件)
          • [5.2.2 读文件](#5.2.2 读文件)

第五章:

5.文件操作

作用 :程序运行时产生的数据都是临时数据,程序运行结束后都会被释放,文件能将数据持久化

对文件进行操作需要包含头文件

两种文件类型

  • 文本文件:以文本ASCII码形式存储在计算机里

  • 二进制文件:以文本二进制形式存储在计算机里

操作文件的三大类:

  • ofstream 写操作

  • ifstream 读操作

  • fstream 读写操作

5.1 文本文件
5.1.1 写文件

步骤

  1. 包含头文件:#include

  2. 创建流对象:ofstream ofs;

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

  4. 写入数据:ofs << "写入的数据"

  5. 关闭文件:ofs.close();

文件打开方式:

打开方式 说明
ios::in 为读文件而打开文件
ios::out 为写文件而打开文件
ios:ate 初始位置为文件尾
ios::app 追加方式写文件
ios::trunc 如果文件存在会先删除,再创建
ios::binary 二进制方式打开文件

文件打开方式可配合使用,用|操作符即可。

如用二进制方式写文件:ios::binary | ios::out

c++ 复制代码
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ofstream ofs;
	ofs.open("text.txt", ios::out);
	ofs << "hello world" << endl;
	ofs << "hello C++" << endl;
	ofs.close();
	system("pause");
	return 0;
}
5.1.2 读文件

步骤

  1. 包含头文件:#include

  2. 创建流对象:ifstream ifs;

  3. 打开文件并判断打开是否成功:ifs.open("文件路径",打开方式)

  4. 读取数据:四种方式,代码中会说明

  5. 关闭文件:ifs.close();

c++ 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream ifs;
	ifs.open("text.txt", ios::in);
	if (!ifs.is_open())  //判断文件是否打开成功
	{
		cout << "打开文件失败!" << endl;
		return 0;
	}

	//读取数据
	//第一种方式
	/*char buf[1024] = { 0 };
	while (ifs >> buf)
	{
		cout << buf << endl;
	}
	system("pause");*/
	
	//第二种方式
	/*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();
	return 0;
}
5.2 二进制文件

打开方式指定:ios::binary

5.2.1 写文件

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

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

说明:字符指针buffer指向内存中一段存储空间。len是读写的字节数。

c++ 复制代码
#include <iostream>
#include <fstream>
using namespace std;

class Student
{
public:
	char name[20];
	int age;
};

int main()
{
	ofstream ofs("stuInfo.txt", ios::out | ios::binary);
	Student s = { "小明", 18 };
	ofs.write((const char*)&s, sizeof(Student));
	ofs.close();
	system("pause");
	return 0;
}
5.2.2 读文件

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

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

说明:字符指针buffer指向内存中一段存储空间。len是读写的字节数。

c++ 复制代码
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Student
{
public:
	char name[20] ;
	int age;
};

int main()
{
	ifstream ifs;
	ifs.open("stuInfo.txt", ios::binary | ios::in);
	if (!ifs.is_open())
	{
		cout << "打开文件失败!" << endl;
		return 0;
	}
	Student s;
	ifs.read((char*)&s, sizeof(s));
	cout << "姓名:" << s.name << " 年龄:" << s.age << endl;
	ifs.close();
	system("system");
	return 0;
f(s));
	cout << "姓名:" << s.name << " 年龄:" << s.age << endl;
	ifs.close();
	system("system");
	return 0;
}
相关推荐
kfepiza25 分钟前
JavaScript将String转为base64 笔记250802
开发语言·javascript·笔记
Warren9827 分钟前
Vue2博客项目笔记(第一天)
java·开发语言·javascript·vue.js·windows·笔记·ecmascript
软件测试-阿涛1 小时前
【自动化测试】Python Selenium 自动化测试元素定位专业教程
开发语言·python·selenium·自动化
幽迷狂1 小时前
AFSIM入门教程03.03:更新所有依赖库版本
c++·qt·仿真·osgearth·osg·军事·afsim
勇闯逆流河2 小时前
【C++】Stack and Queue and Functor
开发语言·c++
来来走走3 小时前
Flutter开发 了解Scaffold
android·开发语言·flutter
xiangweiqiang4 小时前
用phpstudy安装php8.2后报错:意思是找不到php_redis.dll拓展时
开发语言·php
mitt_4 小时前
go语言变量
开发语言·后端·golang
TravisBytes5 小时前
gRPC C++ 从 0 到 1 → 到线上:**超详细** 环境搭建、编码范式、性能调优与 DevOps 全攻略
开发语言·c++·devops
kngines5 小时前
【Node.js从 0 到 1:入门实战与项目驱动】1.1 什么是 Node.js?(定义、运行环境、与浏览器 JavaScript 的区别)
开发语言·javascript·node.js