c++~第9课-----文件操作

目录

文件操作

文本文件

二进制文件

文件指针定位


文件操作

  • 包含头文件<fstream>
  • 文件类型分两种:

文本文件:文件以文本的ASCII码形式存储在计算机中

二进制文件:文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们

文本文件

写文件步骤如下:
包含头文件
#include <fstream>
创建流对象
ofstream ofs;
打开文件
ofs.open("文件路径",打开方式);
写数据
ofs << "写入的数据";
关闭文件
ofs.close();

#include <iostream>
#include <string>
#include <fstream>//包含头文件
using namespace std;
int main()
{
	//1.包含头文件
	//2.创建流对象
	ofstream ofs;
	//3.指定打开方式
	ofs.open("1.txt", ios::out);//如果没有文件自己会先创建文件
	//4.写入数据
	ofs << "2021年2月6日" << endl;//换行符也有用
	ofs << "姓名:张三" << endl;
	ofs << "电话:1234567" << endl;
	//5.关闭文件
	ofs.close();
	return 0;
}

文件打开方式:

 注意:文件打开方式可以配合使用,利用 | 操作符

读文件步骤如下:
包含头文件
#include <fstream>
创建流对象
ifstream ifs;
打开文件并判断文件是否打开成功
ifs.open("文件路径",打开方式);
读数据
四种方式读取
关闭文件
ifs.close();

#include <iostream>
#include <string>
#include <fstream>//包含头文件
using namespace std;
int main()
{
	//1.包含头文件
	//2.创建流对象
	ifstream ifs;
	//3.指定打开方式
	ifs.open("1.txt", ios::in);
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;
		exit(0);
	}
	//4.读取数据(4重方式)
	char buf2[1024] = { 0 };
	while (ifs.getline(buf2, 1024))
	{
		cout << buf2<<endl;//需要手动添加换行
	}
	//5.关闭文件
	ifs.close();
	return 0;
}

读取文件4种方式:

//第一种(遇到空格会换行)空格都被endl回车代替了
	char buf[1024] = { 0 };
	while (ifs>>buf)
	{
		cout << buf<<endl;
	}
//第二种(空格也会输出)
	char buf2[1024] = { 0 };
	while (ifs.getline(buf2, 1024))
	{
		cout << buf2<<endl;//需要手动添加换行
	}
//第三种(跟第二种一样,参数不同罢了)
	string buf3;
	while (getline(ifs, buf3))
	{
		cout << buf3 << endl;//需要手动添加换行
	}
//第四种
	char c;
	while ((c = ifs.get()) != EOF)
	{
		cout << c;//一个个字符读,不需要手动加换行,但是效率低
	}

二进制文件

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

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

//字符指针buffer指向内存中一段存储空间;len是读写的字节数
ostream& write(const char *buffer,int len);
  • 把string写入文件中,需要先转换为char*再写进去

写文件:

#include <iostream>
#include <string>
#include <fstream>//包含头文件
using namespace std;
class MM
{
public:
    char name[20];
	int age;
};
int main()
{
	//1.包含头文件
	//2.创建流对象并且打开文件
	ofstream ofs("1.txt", ios::out | ios::binary);//两步合一步,不用再写open
	//4.写入文件
	MM mm={ "小芳",11 };
	ofs.write((const char*)&mm,sizeof(MM));
	ofs.close();
	return 0;
}

读文件:

#include <iostream>
#include <string>
#include <fstream>//包含头文件
using namespace std;
class MM
{
public:
    char name[20];
	int age;
};
int main()
{
	//1.包含头文件
	//2.创建流对象
	ifstream ifs;
	//打开文件并且判断是否成功打开
	ifs.open("1.txt", ios::in | ios::binary);
	if (!ifs.is_open())
	{
		cout << "打开文件失败" << endl;
		exit(0);
	}
	//4.读文件
	MM mm;
	ifs.read((char*)&mm, sizeof(mm));
	cout << mm.name<<" "<<mm.age << endl;
	//关闭文件
	ifs.close();
	return 0;
}

文件指针定位

  • ifstream类的对象

istream& seekg(long int pos);

istream& seekg(long int pos,ios_base::seekdir position);

  • ofstream类的对象

ostream& seekg(long int pos);

ostream& seekg(long int pos,ios_base::seekdir position);

  • ios_base::seekdir:

ios::beg  文件开始位置

ios::end  文件结束位置

ios::cur   文件当前位置

#include <iostream>
#include <string>
#include <fstream>//包含头文件
using namespace std;
void test(const char* fileName)
{
	fstream fread(fileName, ios::in);
	if (!fread)
	{
		cout << "打开文件失败" << endl;
	}
	char c = fread.get();
	cout << c;
	fread.seekg(4,ios::beg);//打印第4个(文件一开始是在第一个字符上所以读出来是第五个)
	c = fread.get();
	cout << c;
	fread.seekg(-4,ios::end);//打印倒数第四个
	c = fread.get();
	cout << c << endl;
}
int main()
{
	test("1.txt");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luckys-Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值