C++之文件操作

1.C++文件操作

C++中文件操作头文件:fstream。

文件类型:文件文件和二进制文件。

  • 文件操作三大类:
        ofstream 写操作
        ifstream 读操作
        fstream:读写操作
  • 文件打开方式:
标志 说明
ios::in 只读
ios::out 只写,文件不存在则创建,存在则打开并截断原内容
ios::ate 打开一个已有的文件,并指向文件读指针指向文件尾,若文件不存在,则打开出错
ios::app 打开文件,从文件尾添加内容,若文件不存在则创建
ios::trunc 打开文件同时会截断原内容,单独使用时与ios::out相同
ios::binary 以二进制方式打开
ios::in|ios::out 打开文件,可读也可写,文件打开时原内容保持不变,若不存在则打开出错
ios::in|ios::out|ios::trunc 打开文件,可读写,会截断原内容,文件不存在则创建

2.文本方式写入示例

cpp 复制代码
#include < iostream >
#include < fstream >
using namespace std;
int main()
{
	/*1.创建文件*/
	ofstream fp;
	fp.open("test.txt",ios::out);//创建文件,会截断原内容
	if (!fp.is_open())//文件打开失败返回false
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}
	fp << "C++文件操作示例!" << endl;
	fp << "写入数据测试" << endl;
	fp << "姓名:IT_阿水" << "t工作方向:" << "嵌入式开发" << "t工作时间:" << "6年" << endl;
	fp.close();//关闭文件
	system("pause");
}

3.文本方式读取示例

C++中读取数据有多种方式实现。

2.1 示例1:重载>>读取

cpp 复制代码
#include < iostream >
#include < fstream >
using namespace std;
int main()
{
	ifstream ifs;
	ifs.open("test.txt",ios::in);//只读方式打开
	if (!ifs.is_open())
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}
	string str;
	while (ifs >> str)//以字符串方式读取
	{
		cout << "str=" << str << endl;;
	}
	//关闭文件
	ifs.close();
	system("pause");
}

2.2 利用成员函数getline读取

cpp 复制代码
#include < iostream >
#include < fstream >
using namespace std;
int main()
{
	ifstream ifs;
	ifs.open("test.txt",ios::in);//只读方式打开
	if (!ifs.is_open())
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}
	//第二种:getline()
	char buff[1024];
	while (ifs.getline(buff, sizeof(buff)))
	{
		cout << "buff=" << buff << endl;
	}
	//关闭文件
	ifs.close();
	system("pause");
}

2.3 单个字符方式读取get()

cpp 复制代码
#include < iostream >
#include < fstream >
using namespace std;
int main()
{
	ifstream ifs;
	ifs.open("test.txt",ios::in);//只读方式打开
	if (!ifs.is_open())
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}
	//第三种:单个字符方式读取
	char c;
	while ((c = ifs.get()) != EOF)
	{
		cout << c;
	}
	//关闭文件
	ifs.close();
	system("pause");
}

4.二进制方式读写示例

  • 二进制数据写入文件
cpp 复制代码
函数:write(const _Elem* _Str, streamsize _Count)
 形参:_Str --写入的内容的起始地址
       _Count  --写入的字节数
  • 二进制数据读取文件
cpp 复制代码
read(_Elem* _Str, streamsize _Count) ;
 形参:_Str --读取内容存放缓冲区
       _Count --要读取的字节数
cpp 复制代码
#include < iostream >
#include < fstream >
#include < cstring >
using namespace std;
class Person
{
public:
	Person() {}
	Person(const char* name, int age)
	{
		strcpy_s(this->name, name);
		this->age = age;
	}
	char name[20];//姓名
	int age;//年龄
};
int main()
{
	/*二进制写入数据示例*/
	fstream fs("test.doc", ios::out | ios::binary);
	if (!fs.is_open())
	{
		cout << "文件创建失败" << endl;
		return 0;
	}
	Person p("小王", 18);
	fs.write((const char *) & p, sizeof(p));//写入内容
	fs.close();//关闭文件
	/*二进制读取数据示例*/
	fs.open("test.doc", ios::in | ios::binary);
	if (!fs.is_open())
	{
		cout << "文件打开失败" << endl;
		return 0;
	}
	Person p2;
	fs.read((char *) & p2, sizeof(p2));
	cout << "读取的内容:" << endl;
	cout << "姓名:" << p2.name < < "t年龄:" << p2.age << endl;
	fs.close();
	system("pause");

}

5.C++文件指针偏移

cpp 复制代码
//C++文件指针偏移
  seekg(pos_type _Pos,ios_base::seekdir _Way)  --用于输入流,偏移位置指针到指定位置
  seekp(pos_type _Pos,ios_base::seekdir _Way)  --用于输出流,偏移位置指针到指定位置
	第一个参数:偏移量
	第二个参数:基于哪个位置
				ios::beg  --文件头
				ios::end  --文件尾
				ios::cur  --当前位置
  streamoff tellg()  --用于输入流,返回当前指针位置,streamoff 是一个long long类型
  streamoff tellp()  --用于输出流,返回当前指针位置
  返回值返回基于文件头的偏移量,字节为单位。失败则返回-1
  • 示例:
cpp 复制代码
#include < iostream >
#include < fstream >
using namespace std;
int main()
{
	ifstream fs;
	fs.open("test.txt", ios::in );//打开文件,不存在则打开失败,不会截断原内容
	if (!fs.is_open())
	{
		cout << "文件打开失败" << endl;
		return 0;
	}
	fs.seekg(0,ios::end);//将文件指针偏移到文件末尾
	char buff[1024];
	streamoff  size = fs.tellg();//获取文件大小
	cout << "文件大小:" << size << "字节" << endl;
	fs.seekg(0, ios::beg);//将输入流偏移到文件头
	while (fs >> buff)
	{
		cout << buff << endl;
	}
	fs.close();
	system("pause");
	return 0;
}
相关推荐
Mr.Z.41115 分钟前
【历年CSP-S复赛第一题】暴力解法与正解合集(2019-2022)
c++
Death20019 分钟前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
郭二哈29 分钟前
C++——list
开发语言·c++·list
黑不溜秋的1 小时前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光1 小时前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
￴ㅤ￴￴ㅤ9527超级帅2 小时前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
_GR2 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
Death2002 小时前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
六点半8882 小时前
【C++】速通涉及 “vector” 的经典OJ编程题
开发语言·c++·算法·青少年编程·推荐算法
coduck_S12004zbj3 小时前
csp-j模拟五补题报告
c++·算法·图论