C++学习——文件 *** 作

C++学习——文件 *** 作,第1张

文件 *** 作

程序运行过程中产生的数据都属于临时数据,程序一旦运行结束就会被删除掉。
可以通过文件存储的方式将数据存储下来。

文件类型分类:

文件文本 —— 文件以文本的ASCII码形式存储在计算机中二进制文件 —— 文件以文本的二进制形式存储在计算机中,(二进制较难直接懂)

C++的头文件的包含:#inclass="superseo">clude(文件流)
文件 *** 作的三大类:

ofsream : 写 *** 作ifstream :读 *** 作fstream :读写 *** 作 读写文本文件的 *** 作(基本步骤) 包含头文件 #include创建流对象:ofstream ofs;打开文件 :ofs.open("文件路径",打开方式);(需要带一个判断语句判断头文件的是否打开成功)写数据:ofs<<" ";(or读数据)关闭文件: ofs.close();//必须做 *** 作

打开方式:

代码说明
ios::in为读文件而打开文件
ios::out为写文件而打开文件
ios::ate初始位置 到 文件尾部
ios::app追加方式写文件(打开的时候直接跑到最后追加写)
ios::trunc如果文件存在先删除,在创建新的
ios::binary二进制方式

注意:

当需要采用多方式配合打开文件时, *** 作:ios::binary | ios::out,即或 *** 作文件的中的换行使用endl也可以 写文件的简单具体代码:
    ofstream ofs;
//若文件存在先删除在创建一个新的,后以写的方式打开,之前没有创建使用open也能创建
	ofs.open("test.txt",ios::out | ios::trunc);
	ofs << "写入文件数据" << endl;
	ofs << "张三,12" << endl;
读文件的简单具体代码:
//创建读文件流
		ifstream ifs;
	//打开文件
		ifs.open("test.txt", ios::in);
	//判断文件是否打开
		if (!ifs.is_open())
		{
			cout << "打开失败"<< endl;
			return;
		}
	//读数据(4种方式)
		char buf[1024] = {0};
		while (ifs >> buf)//当读到文件尾时,会返回false
		{
			cout << buf << endl;
		}
	//关闭文件
		ifs.close();

由于读取方式一般有4种,这里采用类的方式全部给出:

#include
#include
#include
using namespace std;

class folder
{
public:
	folder(){}
	~folder(){}

	void test01()
	{
		ofstream ofs;
		ofs.open("test.txt", ios::out | ios::trunc);//若文件存在先删除在创建一个新的,后以写的方式打开,之前没有创建使用open也能创建
		ofs << "写入文件数据" << endl;
		ofs << "张三,12" << endl;
	}
	void test02()
	{
	//创建读文件流
		//ifstream ifs;
	//打开文件
		IFS.open("test.txt", ios::in);
	//判断文件是否打开
		if (!IFS.is_open())
		{
			cout << "打开失败"<< endl;
			return;
		}
	//读数据(4种方式)
		read_way_4(IFS);
	//关闭文件
		IFS.close();
	}
	void read_way_1(ifstream &ifs)  //方式1
	{
		char buf[1024] = {0};
		while (ifs >> buf)
		{
			cout << buf << endl;
		}
		
		ifs.close();
	}

	void read_way_2(ifstream &ifs)  //方式2
	{
		char buf[1024] = { 0 };
		while (ifs.getline(buf,sizeof(buf)))
		{
			cout << buf << endl;
		}
		ifs.close();
	}

	void read_way_3(ifstream &ifs)//方式3,推荐,长度可扩展
	{
		string buf;
		while (getline(ifs, buf))
		{
			cout << buf << endl;
		}
		ifs.close();
	}
	void read_way_4(ifstream &ifs)//方式4,不推荐,费时间
	{
		char c;
		while ((c = ifs.get())!=EOF)//EOF end of file (文件尾的意思)
		{
			cout << c;
		}
		ifs.close();
	}
private:
	ifstream IFS;
};

int main()
{
	folder f;
	f.test01();//写入数据
	f.test02();//读数据
	
	system("pause");
	return 0;
}
采用二进制方式进行读写文件 打开方式需要指定以 ios::binary方式打开好处:二进制会将各种类型(包括自定义的数据类型)写入文件中注意一下:在使用字符串写入时,最好采用 char[] 数组类型写入,不然会出现一定的问题 二进制写文件

二进制方式文件注意利用流对象调用成员函数 **write(x,x) **

即:函数原型:ostream& write(const char * buffer, int len);
解释:字符指针 buffer 指向内存中一段存储空间,len 是读写的字节数

class folder
{
public:
void test03(person &p)
	{
		ofstream ofs("test_2.txt", ios::out | ios::binary);//以二进制方式打开写入
		
		ofs.write((const char *)&p, sizeof(p));//强制转换成const类型

		ofs.close();

	}
}
class person
{
public:
	char P_Name[64];
	int age;
};
int main()
{
	folder f;
	person p;
	p = { "李四",10 };
	f.test03(p);//二进制写入自定义的person类型数据

	system("pause");
	return 0;
}
读二进制文件

函数原型:ostream& read(char * _Str, int len);

class person
{
public:
	char P_Name[64];
	int age;
};

class folder
{
public:
void test04()//读取二进制数据 *** 作
	{
		ifstream ifs("test_2.txt",ios::in | ios::binary);
		//最好加上一个是否打开的判断
		if (!ifs.is_open())
		{
			cout << "打开失败" << endl;
			return;
		}
		person px;
		ifs.read((char*)&px,sizeof(person));//将读到数据存到px中
		ifs.close();
		cout << "姓名:" << px.P_Name << " 年龄:" << px.age << endl;
	}
}
int main()
{
	folder f;
	person p;
	p = { "李四",10 };
	f.test03(p);//写数据
	f.test04();//读取数据

	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/993747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存