黑马:文件操作143~146

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

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

文件操作需要包含头文件<fstream>

文件类型分为两种:

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

2.二进制文件文件以二进制形式存储在计算机中,用户一般不能读取

操作文件的三大类

1.ofsream:写操作

2.ifstream:读操作

3.fstream:读写操作

1.文本文件文件

 

 

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
//文本文件文件
void test01() {
	//创建流对象
	ofstream ofs;

	//指定打开方式
	ofs.open("test.txt", ios::out);

	//写内容
	ofs << "姓名: 徐吉磊" << endl;
	ofs << "性别: 男" << endl;
	ofs << "年龄: 20" << endl;

	//关闭文件
	ofs.close();


}

int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

运行之后什么也没有,要在运行按钮那里右键,打开文件夹位置才可以找到。

2.文本文件文件

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>


//文本文件文件
void test01() {
	//创建流对象
	ifstream ifs;

	//指定打开方式,判断是否打开成功
	ifs.open("test.txt", ios::in);
	if (!ifs.is_open()) {
		cout << "打开文件失败" << endl;
		return;
	}

	//读数据

	//第一种
	/*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;
	}


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


}

int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

is_open是个函数
 

3.二进制文件文件

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>


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

};

void test01() {
	/*ofstream ofs;
	ofs.open("Person./txt", ios::out | ios::binary);*/

	ofstream ofs("Person.txt", ios::out | ios::binary); //这样写也可以 缩减为一行

	Person p = { "张三",18 };
	ofs.write((const char*)&p,sizeof(Person));

	ofs.close();


}


int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

 

4.二进制文件文件

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>


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

};

void test01() {

	ifstream ifs;
	 
	ifs.open("Person.txt", ios::in | ios::binary);

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

	Person p;

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

	ifs.close();


}


int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...