C++ 文件操作

文件操作

复制代码
// 文件操作
// 程序运行时产生的数据都属于临时数据,程序结束后临时数据会被操作系统释放
// 通过文件操作可以将数据持久化
// c++ 中文件操作需要包含头文件 <fstream>

// 文件类型分为两种:
// 文本文件: 文件以文本的ASCII码形式存储在计算机中
// 二进制文件: 文件以二进制形式存储在计算机中,任何文本编辑器都不能查看

// 操作文件的三个类
// ofstream  写操作
// ifstream  读操作
// fstream   读写操作

1. 文本文件

1.1 写文件

复制代码
写文件步骤:
1. 包含头文件
#include <fstream>
2. 创建流对象
ofstream ofs;
3. 打开文件
ofs.open("文件名", ios::out);
4. 写数据
ofs << "写入的数据" << endl;
5. 关闭文件
ofs.close()
csharp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


// 文本文件写文件


void test01() {
    // 1.包含头文件 #include <fstream>
    // 2.创建流对象
    ofstream ofs;
    // 3.打开文件
    ofs.open("test.txt", ios::out | ios::app);
    // 4.写数据
    ofs << "name: zhangsan 11" << endl;
    ofs << "sex: man" << endl;
    ofs << "age: 18" << endl;
    // 5.关闭文件
    ofs.close();
}

int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

12. 读文件

csharp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


// 文本文件读文件
// 读数据的四种方式
void test01() {
    // 1. 包含头文件
    // 2. 创建流对象
    ifstream ifs;
    // 3. 打开文件
    ifs.open("test.txt", ios::in);
    //判断文件是否打开
    if (!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return;
    }
    // 4. 读数据 
    // 第一种方式
    // 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 = '\0';
    // 判断文件是否读完
    while((c = ifs.get()) != EOF) {
        cout << c;
    }

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

int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

2 二进制文件

复制代码
以二进制文件对文件进行读写操作
打开方式要指定为ios:binary

2.1 写文件

复制代码
写文件
二进制方式写文件主要是利用流对象调用成员write()方法
函数原型:ostream& write(const char* s, int len);
s:指向要写入的字符串的指针
len:写入的字符串长度
cpp 复制代码
// main.cpp
#include <iostream>
#include <fstream>
using namespace std;

// 二进制 写文件
class Person {
public:
    char m_name[64]; // 姓名
    int m_age;       // 年龄
};

void test01() {
    // 1.包含头文件
    // 2.创建流对象
    ofstream ofs("person.txt", ios::out | ios::binary);
    // 3.打开文件
    // ofs.open("person.txt",ios::out | ios::binary);
    // 4.写文件
    Person p = {"张三", 18};
    ofs.write((const char*)&p, sizeof(Person));

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


int main() {
    test01();
    return 0;
}

2.2 读文件

cpp 复制代码
// main.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 二进制 读文件
// 二进制读文件主要是利用 istream的read函数
// 函数原型 istream& read(char* buf, int len);

class Person {
public:
    char m_name[64]; // 姓名
    int m_age;       // 年龄
};

void test01() {
    // 1.创建流对象
    ifstream ifs;
    // 2.打开文件
    ifs.open("person.txt", ios::in | ios::binary);
    if (!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return;
    }
    // 3.读文件
    Person p;
    while (ifs.read((char*)&p, sizeof(Person))) {
        cout << "name:" << p.m_name << " age: " << p.m_age << endl;
    }
    // 4. 关闭文件
    ifs.close();
}


int main() {
    test01();
    return 0;
}
相关推荐
阿珊和她的猫2 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
fouryears_234174 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~5 小时前
C#---StopWatch类
开发语言·c#
lifallen6 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研6 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
快乐的划水a6 小时前
组合模式及优化
c++·设计模式·组合模式
cui__OaO7 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
星星火柴9367 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
鱼鱼说测试8 小时前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
艾莉丝努力练剑8 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法