# day-10 C++文件操作

文本文件

程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放

通过文件可以将数据持久化

C++中对文件操作需要包含头文件 < fstream >

文件类型分为两种:

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

操作文件的三大类:(不要写反ofstream和ifstream)

  1. ofstream:写操作
  2. ifstream: 读操作
  3. fstream : 读写操作

写文件

写文件步骤如下:

  1. 包含头文件
    #include <fstream>

  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

代码:

#include <iostream>
using namespace std;
#include <fstream>

void test() {
    
    
	ofstream ofs;
	ofs.open("test.txt", ios::out);
	ofs << "阿董" << endl;
	ofs << "19岁" << endl;
	ofs << "是个仙女" << endl;
	ofs.close();
}

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

运行结果:
控制台中没有输出任何结果
在这里插入图片描述
右击
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

读文件

读文件步骤:

  1. 包含头文件
    #include <fstream>

  2. 创建流对象
    ifstream ifs;

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

  4. 读数据
    四种方式读取

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

代码:

#include <iostream>
using namespace std;
#include <fstream>
#include <string>

void test(){
    
    
	ifstream ifs;
	ifs.open("test.txt", ios::in);

	if (!ifs.is_open()){
    
    
		cout << "文件打开失败" << endl;
		return;
	}


第四步:有四种方法,建议第三种



	ifs.close();

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

第一种:

	char buf[1024] = {
    
     0 };
	while (ifs >> buf){
    
    
		cout << buf << endl;
	}

第二种

	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){
    
    	//EOF:end of file
		cout << c ;//不能加endl,否则会乱码或者读不出来
	}

运行结果:
若找不到文件
在这里插入图片描述

若找到:
在这里插入图片描述

  • 利用is_open函数可以判断文件是否打开成功
  • if ( ! ifs.is_open)的意思是 if(ifs.is_open() = = false)

二进制文件

写文件

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

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

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

代码:

#include <iostream>
using namespace std;
#include <fstream>
#include <string>

class Person{
    
    
public:
	char m_Name[64];
	int m_Age;
	char m_Type[64];
};

void test(){
    
    
	ofstream ofs;//	等于ofstream ofs("person.txt", ios::out | ios::binary);

	ofs.open("person.txt", ios::out | ios::binary);

	Person p = {
    
     "阿董"  , 19, "是个仙女"};

	ofs.write((const char*)&p, sizeof(p));

	ofs.close();
}



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

运行结果:
在这里插入图片描述
右击
在这里插入图片描述
!](https://img-blog.csdnimg.cn/20210130173310962.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80ODI0NTE2MQ==,size_16,color_FFFFFF,t_70)

在这里插入图片描述
发现是乱码的不用理,只要后序读进去是对的就行
在这里插入图片描述

读文件

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

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

参数解释:
字符指针buffer指向内存中一段存储空间
len是读写的字节数

代码:

#include <iostream>
using namespace std;
#include <fstream>
#include <string>

class Person{
    
    
public:
	char m_Name[64];
	int m_Age;
	char m_Type[64];
};

void test(){
    
    
	ifstream ifs;//	等于ifstream ifs("person.txt", ios::out | ios::binary);

	ifs.open("person.txt", ios::in | ios::binary);
	if (!ifs.is_open()){
    
    
		cout << "文件打开失败" << endl;
	}

	Person p;

	ifs.read((char*)&p, sizeof(p));
	cout << "姓名: " << p.m_Name << endl;
	cout << "年龄: " << p.m_Age  << endl;
	cout << "类型: " << p.m_Type << endl;

	ifs.close();
}



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

运行结果:
若找不到文件在这里插入图片描述
若找到
在这里插入图片描述

注意
1、文件读取失败时,一定要记得return;
否则:在这里插入图片描述
2、二进制写文件有const,读没有

ofs.write((const char*)&p, sizeof(p));

ifs.read((char*)&p, sizeof(p));

3、
in>>noskipws; //读入字符时,不忽略空格 ,逐字符读取
ifstream in(file); //从外部文件读取文本
dataset.clear();
//DataSet.Clear 方法: 通过移除所有表中的所有行来清除任何数据的 DataSet
in>>skipws; //恢复默认值
in.close();

猜你喜欢

转载自blog.csdn.net/weixin_48245161/article/details/113437964