C++文件操作 写文件 读文件 文本文件与二进制文件(超详细)学习笔记

知识点介绍: 

 程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放
 通过文件可以将数据持久化
 c++中对文件的操作需要包含头文件 <fstream> 
 文本文件分为两种:

1.文本文件 文件以文本的ASCLL码形式存储在计算机中

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

操作文件的三大类:

1.ofstaeam 写操作

2.ifstream 读操作

3.fstram 读写操作

文件打开方式:

1. ios::in为读文件而打开文件 
2. ios::out 为写文件而打开文件 
3. ios::ate 初始位置:文件尾
4. ios::trunc 如果文件存在 先删除 再创建
5. ios::binary 二进制方式 


一 .文本文件操作

1.写文件

 //文本文件 写文件
#include<iostream>
using namespace std;                    
#include<fstream>
void test() 
{
	//1.包含头文件<fstream>                                         

	//2.创建流对象 
	ofstream ofs;
	//3.指定打开方式
    //调用open函数 
    //open(const char *_Filename, std::ios_base::openmode _Mode,int _Prot = 64)
    //open函数的参数(文本路径,打开方式)
	ofs.open("test.txt", ios::out);
	//4.写内容
	ofs << "姓名:张三" << endl;
	ofs << "年龄:18" << endl;
	ofs << "性别:男" << endl;
	//5.关闭文件
	ofs.close();
}
int main()
{
	test();
	system("pause");
 return 0;
}

运行结果为:

注意:open(const char *_Filename, std::ios_base::openmode _Mode,int _Prot = 64)
open(文本路径,打开方式) 既可以写文本的路径 也可以直接写文件名 会默认指向与源文件最近的文件 

此时传入了文本文件之中 无法显示  可以从打开文本文件查看内容:

2.读文件

#include<iostream>
using namespace std;                                                    //读文件
#include<fstream>
#include<string>
void test()
{
	//1.包含头文件
	//2.创建流对象
	ifstream ifs;
	//3.打开文件 并且判断是否成功
	ifs.open("test.txt", ios::in);
	//成员函数is_open 作用:是否打开文件成功 为bool类型
	if (!ifs.is_open())
	{
		cout << "打开文件失败" << endl;
		return;
	}
	//4.读数据

	//定义一个字符串变量
	string buf;
	//调用getline函数得到文件数据并传给buf字符串变量 在此之前 必须包含头文件 <string>
	while (getline(ifs, buf))
	{
        //将buf变量的数据输出
		cout << buf << endl;
	}

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

}
int main()
{
	test();
	system("pause");
	return 0;
}

运行结果:

注意:getline(std::istream &_Istr, std::string &_Str)  getline(流对象,字符串变量) 返回的是bool类型

二. 二进制文件操作

1.写文件

//写文件
#include<iostream>
using namespace std;
#include<fstream>
//定义一个person类
class person
{
public:
	char m_Name[64];  //二进制方式里 用c++里的string类型 会出现问题 所以用 char型代替
	int m_Age;
};
void test()
{
    //1.包含头文件

	//2.创建流对象
	ofstream ofs;
	//3.打开文件
	ofs.open("test.txt", ios::out | ios::binary);
    //需用换位符"|" 加入 binary 表示也使用了二进制
	//4.写文件
	//初始化对象数据 
	person p = {"李四",20};
	ofs.write((const char*)&p, sizeof(person));
//关键: 指出p的地址 &p 此时为 person * 类型 要强转为const char * 类型
	//5.关闭文件
	ofs.close();

}
int main()
{
	test();
	system("pause");
	return 0;
}

通过文本文件查看内容:

此时既有文本文件方式 也有二进制方式 二进制方式为乱码无法直接看出

运行结果:

注意:二进制方式写文件主要利用流对象调用成员函数 write
函数原型 ostream& write(const char * buffer,int len)  buffer:缓冲区
参数解释 字符指针buffer指向内存中一段储存空间,len是读写的字节数


2.读文件

//读文件
#include<iostream>
using namespace std;
#include<fstream>
//定义一个person类
class person
{
public:
	char m_Name[64];
	int m_Age;
};
void test()
{
	//1.包含头文件
	
	//2.创建流对象
	ifstream ifs;
    //3.打开文件 并判断是否成功
	ifs.open("test.txt", ios::in | ios::binary);
    //需用换位符"|" 加入 binary 表示也使用了二进制
	if (!ifs.is_open())
	{
		cout << "打开文件失败" << endl;
		return;
	}
	//4.读文件
    //创建对象p
	person p;
	ifs.read((char*)&p, sizeof(person));
    //关键: 指出p的地址 &p 此时为 person * 类型 要强转为 char * 类型 
    //将文件数据传入给p 并将p的数据输出
	cout << "姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
	//5.关闭文件
	ifs.close();
}
int main()
{
	test();
	system("pause");
	return 0;
}

运行结果:

注意:二进制方式读文件主要利用流对象调用成员函数 read
函数原型 ostream& read(char * buffer,int len)  buffer:缓冲区
参数解释 字符指针buffer指向内存中一段储存空间,len是读写的字节数

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是代码分析: 文本文件: ```C++ #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin("test.txt"); if (!fin) { cout << "文件打开失败!" << endl; return 1; } char ch; while (fin >> ch) { // 取字符 cout << ch; } fin.close(); return 0; } ``` 这段代码使用了C++的iostream库中的ifstream类文本文件。首先打开文本文件,然后使用while循环逐个取字符,最后关闭文件文本文件: ```C++ #include <iostream> #include <fstream> using namespace std; int main() { ofstream fout("test.txt"); if (!fout) { cout << "文件打开失败!" << endl; return 1; } fout << "Hello, World!" << endl; // 入字符串 fout.close(); return 0; } ``` 这段代码使用了C++的iostream库中的ofstream类文本文件。首先打开文本文件,然后使用<<运算符入字符串,最后关闭文件二进制文件: ```C++ #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin("test.dat", ios::in | ios::binary); if (!fin) { cout << "文件打开失败!" << endl; return 1; } char ch; while (fin.read(&ch, sizeof(ch))) { // 取一个字节 cout << ch; } fin.close(); return 0; } ``` 这段代码使用了C++的iostream库中的ifstream类二进制文件。首先以二进制方式打开文件,然后使用while循环逐个取一个字节,最后关闭文件二进制文件: ```C++ #include <iostream> #include <fstream> using namespace std; int main() { ofstream fout("test.dat", ios::out | ios::binary); if (!fout) { cout << "文件打开失败!" << endl; return 1; } char str[] = "Hello, World!"; fout.write(str, sizeof(str)); // 入字符串 fout.close(); return 0; } ``` 这段代码使用了C++的iostream库中的ofstream类二进制文件。首先以二进制方式打开文件,然后使用write()函数入字符串,最后关闭文件。 总的来说,文本文件二进制文件的区别在于打开方式和的方式不同。在文本文件时,使用默认的方式即可;在二进制文件时,则需要使用二进制方式打开,并使用read()和write()函数进行操作

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小苏先生.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值