【C++第二阶段】文件操作

以下内容仅为当前认识,可能有不足之处,欢迎讨论!



文件操作

文件写入流程

写文件包括以下几个步骤

1.包含头文件

2.创建流对象

3.打开文件,以指定方式

4.写入内容

5.关闭文件

1.头文件一般用

ofstream(写文件) ——从编译器中向文件写,故为output-file-stream,

ifstream(读文件)——从文件中向编译器读,故为in-file-stream,

fstream(读写文件)——从文件中可以读出数据,也可以将编译器中的数据写入文件,故为file-stream。

流对象一般用对应头文件的对象。

指定方式有表格,可以指定多种,用|来分割。

模式标志描述
ios::app追加模式。所有写入都追加到文件末尾
ios::ate文件打开后定位到文件末尾
ios::in打开文件用于读取
ios::out打开文件用于写入
io::trunc如果该文件已经存在,内容将在打开文件之前被截断,即把文件长度设置为0

写入内容是左移运算符

关闭文件一般是close()函数

简单的demo写操作

代码

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

void test0226_0() {
	/*
	写文件包括以下几个步骤
	1.包含头文件
	2.创建流对象
	3.打开文件,以指定方式
	4.写入内容
	5.关闭文件

	头文件一般用ofstream , ifstream , fstream
	流对象一般用对应头文件的对象
	指定方式有表格,可以指定多种,用|来分割
	写入内容是左移运算符
	关闭文件一般是close()函数
	*/
	//1.创建流对象
	fstream fs;
	//2.打开文件及指定打开方式
	fs.open("file.txt", ios::app);
	cout << "打开成功文件成功,对其写入数据." << endl;
	fs << "打开一个文件,对其写入" << endl;
	cout << "写入文件成功,关闭文件." << endl;
	fs.close();
	cout << "关闭文件成功." << endl;

	
}

int main() {
	cout << "hello ! world ! " << endl;
	test0226_0();
    system("pause");
    return 0;}

运行结果:

image-20240226094431017

image-20240226094446172

可以看到成功写入。

文件读流程

文件读流程有4种方式。

①通过右移运算符放入字符数组中;

ifstream ifs;
ifs.open("file.txt",ios::in);
if (!ifs.is_open()){
    return;
}
char char_arry[1024]={0};
while (ifs>>char_array){
    cout<<char_array<<endl;
}
ifs.close();

②通过文件流对象自带的getline函数用字符数组逐行接收;

ifstream ifs;
ifs.open("file.txt",ios::in);
if(!ifs.is_open()){
	return ;
}
char char_array[1024]={0};
while (ifs.getline(char_array,sizeof(char_array))){
    cout<<char_array<<endl;
}
ifs.close();

③通过string头文件自带的全局getline函数用string函数接收;

ifstream ifs;
ifs.open("file.txt" , ios::in);
if(!ifs.is_open()){
    return ;
}
string array;
while(getline(ifs , array)){
    cout<<array<<endl;
}
ifs.close();

④通过文件流对象逐个读取字符打印。

ifstream ifs;
ifs.open("file.txt" , ios::in);
if(!ifs.is_open()){
    return ;
}
char c;
while(c=ifs.get() && ifs.get()!= EOF){
    cout<<c<<endl;
}
ifs.close();

最后种方式我打印不出来。

image-20240226110648748

二进制写文件

二进制方式对文件写入

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

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

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

在用二进制方式写文件时,不必拘泥于固有的数据类型,也可以写入自定义的数据类型,比如类。但对于字符串,最好还是用char来写,因为底层是用C实现的。

代码实现:

#include<iostream>
#include<fstream>
using namespace std;
class Person{
    public:
    	char name[1024];
    	int age;
};
void test0226(){
    //1.写入头文件
    //2.定义文件流
    fstream fs;
    //3.打开文件
    fs.open("Person.txt" , ios::out | ios::binary);
    //这里可以直接写成,有对应的构造函数
    //fstream fs("Person.txt" , ios::in | ios::binary);
    //4.写入数据
    Person person={"张三",20} ;
    //person.name = "张三";//这里不对,不能这样写
    //person.age = 20;
    fs.write((const char *)&person , sizeof(person));
    //这里必须用强制类型转换,为什么要用引用,因为要获取地址
    fs.close();
}

可以看到确实有这个文件

image-20240226114134310

二进制读文件

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

代码:

#include<iostream>
#include<fstream>
using namespace std;
class Person{
    public:
    char name[64];
    int age;
};
void test0226_3() {
	ifstream inBuffer;
	inBuffer.open("Person.txt", ios::in | ios::binary);
	if (!inBuffer.is_open()) {
		return;
	}
	Person person;
	inBuffer.read((char*)&person, sizeof(person));
	cout << "person.name = " << person.name << ",person.age = " << person.age << "." << endl;
	inBuffer.close();
}

int main() {
	cout << "hello ! world ! " << endl;
	test0226_3();
    system("pause");
    return 0;
}

运行结果如下

image-20240226115140401


以上是我的学习笔记,希望对你有所帮助!
如有不当之处欢迎指出!谢谢!

学吧,学无止境,太深了

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

HelpFireCode

随缘惜缘不攀缘。

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

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

打赏作者

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

抵扣说明:

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

余额充值