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;
}
相关推荐
晚风吹长发1 天前
二分查找算法+题目详解
c++·算法·二分查找
怪兽20141 天前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
ClearLiang1 天前
Kotlin-协程的挂起与恢复
开发语言·kotlin
彭同学学习日志1 天前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
海域云赵从友1 天前
破解跨境数据传输瓶颈:中国德国高速跨境组网专线与本地化 IP 的协同策略
开发语言·php
咚咚王者1 天前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
罗义凯1 天前
其中包含了三种排序算法的注释版本(冒泡排序、选择排序、插入排序),但当前只实现了数组的输入和输出功能。
数据结构·c++·算法
深蓝海拓1 天前
使matplot显示支持中文和负号
开发语言·python
春蕾夏荷_7282977251 天前
c++ easylogging 使用示例
c++·log·easylogging
syt_biancheng1 天前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法