C++文件操作相关(笔记自用

C++文件操作相关(笔记自用

c++文件操作相关需要包含相关头文件如下

#include<fstream>

先了解文件类型
1,文本文件:文件以文本的ASCII码形式存储在计算机中(人能看懂
2,二进制文件:文件以文本的二进制形式存储在计算机中(一般人是看不懂的,但对于计算机而言更好理解

接下来是需要了解的用于文件操作的语法
1,ofstream 写操作
2,ifstream 读操作
3,fstream 读写操作

以下为我朋友让我修改后的一串代码,我就直接拿来用了
涉及了粗浅的文件读取与写入
这部分代码主要是将as.txt中的学生信息录入到for.txt文本当中

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
class Student {
	public:
		Student(string n, int a, string s):name(n),age(a),school(s){}
		Student():name(""),age(0),school(""){}
		string name;
		int age;
		string school;
	};
ostream& operator<<(ostream& out, const Student& stu) {
	out << stu.name << " " << stu.age << " " << stu.school;
	return out;
}
istream& operator>>(istream& in, Student& stu) {
	in >> stu.name >> stu.age >> stu.school;
	if (!in) {
		stu = Student();
	}
	return in;
}
int main() {
	ifstream infile("as.txt");
	if (!infile) {
		cerr << "输入文件无法打开" << endl;
		return -1;
	}
	vector<Student>students;
	Student stu;
	while (infile >> stu) {
		students.push_back(stu);
	}
	infile.close();
	ofstream outfile("for.txt");
	if (!outfile) {
		cerr << "输出文件无法被打开!" << endl;
		return -1;
	}
	for (int i = 0; i < students.size(); i++) {
		outfile << students[i] << endl;
	}
	outfile.close();
	return 0;
}

顺带一提,上面的代码仅供理解,因为我们打开文件一般是需要用open函数来打开的
步骤如下
1,包含<fstream>头文件
2,创建流对象
(备注,可以在创建对象的时候就确定打开的文件以及打开方式,上面的代码就是采取这种方法)

ofstream outfile("as.txt",ios::out);

例如 :

ifstream ifs;
ofstream ofs;
fstream fs;

3,打开文件
记得要判断是否成功打开文件

ofs.open("文件路径",打开方法);
ifs.open("文件路径",打开方法)if(!ifs.is_open())
{
cout<<"打开文件失败"<<endl;
return -1;
}
if(!ofs.is_open())
{
cout<<"打开文件失败"<<endl;
return -1;
}

判断是否打开成功可以使用is_open函数
4,读写数据

ofs<<"写入的数据";
ifs>>"输出的数据";

相比写,读取数据拥有更多种的方法
第一种:

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

第二种:(行获取方法

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

第三种:

string buf;
while(getline(ifs,buf))
{
cout<<buf<<endl;
}

第四种:(不推荐使用,一个个读取速度慢

char c;
while((c=ifs,get())!=EOF)
{
cout<<c;
}
在这里插入代码片

5,关闭文件

ofs.close();
ifs.close()

文件的打开方式如下

打开方式作用
ios::in为读取文件而打开文件
ios::out为写文件而打开文件
ios::ate返回初始位置:文件尾
ios::app续写文件
ios::trunc如果文件存在,先删除后创建
ios::binary二进制方式

注意:可以利用|操作符配合着使用文件打开方式
如下,用二进制方式写文件

ios::binary|ios::out

我的记忆方法,以计算机为本体,out是从本体进入文本,就是写,in则是进来,从文本进入计算机,为读。

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

二进制方式读写文件的功能更强大,甚至可以对自定义类型进行读写
此时使用字符串时最好不用c++的string改用c的char以防出现问题

二进制写文件使用write

class person
{
public:
char name[64];
int age;
}
int main()
{
ofstream ofs("shiyan.txt",ios::binary|ios::out);
person p={"zhangsan",18};
ofs.write((const char*)&p,sizeof(person));
//函数第一个参数需要时char类型的指针,而我们传入的是自定义的类,需要强行转换成char类型的数据
}

最后不能忘记要关闭文件,二进制与文本文件关闭方法相同
但是此时打开文件,有大可能会是乱码,但是不重要,二进制文本文件本来就不是给程序员看的,只需要计算机能读懂即可。

二进制读文件如下
函数原型 ifstream& read(char*buffer,int len);
参数解释:字符指针buffer指向内存中的一段存储空间,len是读写的字节数

class person
{
public:
char name[64];
int age;
}
int main()
{
ifstream ifs("shiyan.txt",ios::binary|ios::in);
if(!ifs.is_open())
{
cout<<"打开文件失败"<<endl;
return -1;
}
person p;
ifs.read((char*)&p,sizeof(person));
cout<<"姓名:"<<p.name<<"年龄:"<<p.age<<endl;
ifs.close();
return 0;
}

所有笔记均学习自:http://yun.itheima.com/course/520.html?bili

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值