• C++ 文件操作


    文件类型

    1. 文本文件 - 文件以文本的ASCII码形式存储在计算机中
    2. 二进制文件 - 文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们

    操作文件的三大类

    1. ofstream: 写操作
    2. ifstream: 读操作
    3. fstream: 读写操作
      理解:你站在你程序的视角上就行了 从程序中写入到文件中 就是output – >ofstream 相反同理

    文本读写

    写文件

    1. 包含头文件#include
    2. 创建流对象ofstream ofs
    3. 打开文件ofs.open("文件路径", 打开方式)
    4. 写数据 ofs << "写入的数据"
    5. 关闭文件 ofs.close()
    打开方式解释
    ios::in为读文件而打开文件
    ios::out为写文件而打开文件
    ios::ate初始位置:文件尾
    ios::app追加方式写文件
    ios::trunc如果文件存在先删除,再创建
    ios::binary二进制方式

    如果需要几种配合使用,使用|操作符
    例如:ios::binary | ios::out

    void o_op()
    {
        ofstream ofs;
        
        string f_url = "/Users/###/Desktop/123.txt";
        
        ofs.open(f_url,ios::out);
        
        ofs << "姓名: 张三" << endl;
        ofs << "年龄:18" << endl;
        
        ofs.close();
        
        cout << "end" << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    读文件

    读文件与写文件步骤相似,但是读取方式相对比较多

    1. 包含头文件#include
    2. 创建流对象ifstream ifs
    3. 打开文件,并判断文件是否打开成功ifs.open("文件路径", 打开方式)
    4. 读数据,四种方式
    5. 关闭文件 ifs.close()
    void i_op()
    {
        ifstream ifs;
        
        string f_url = "/Users/###/Desktop/123.txt";
        
        ifs.open(f_url,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;
    //    }
        
        // 第四种:一个一个读,效率比较低; EOF -> end of file
    //    char c;
    //    while ((c = ifs.get()) != EOF)
    //    {
    //        cout << c;
    //    }
    //
    //    ifs.close();
        
        cout << "end" << endl;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    二进制文件读写

    写文件

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

    class Person
    {
    public:
        char m_Name[64]; // 姓名
        int m_Age; // 年龄
    };
    
    void testWrite()
    {
        ofstream ofs;
        
        string f_url = "/Users/runhe_ios_jh/Desktop/1234.txt";
        
        ofs.open(f_url, ios::out | ios::binary);
        
        Person p = {"李四", 10};
        ofs.write((const char *)&p, sizeof(Person));
        
        ofs.close();
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    读文件

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

    void testRead()
    {
        ifstream ifs;
        
        string f_url = "/Users/runhe_ios_jh/Desktop/1234.txt";
        
        ifs.open(f_url, 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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    力扣labuladong——一刷day02
    cppcheck使用大全
    时间戳计算年-月-日 时:分
    城市大脑 术语
    Java abstract 关键字(keyword)
    使用requests库解决Session对象设置超时的问题
    2023年浙江省内MBA项目报考选择策略分析
    Linux中的dpkg指令(dpkg -l | grep XXX等)
    使用springboot实现jsonp|jsonp的实现|JSONP的实现使用springboot
    力扣:152. 乘积最大子数组(Python3)
  • 原文地址:https://blog.csdn.net/JH_Cao/article/details/126531384