• C++文件操作->文本文件(->写文件、读文件)、二进制文件(->写文件、读文件)


    #include
    using namespace std;
    #include //头文件包含

    //文本文件 写文件
    void test01()
    {
        //1.包含头文件  fstream

        //2.创建流对象
        ofstream ofs;
        //3.指定打开方式
        ofs.open("test.txt", ios::out);
        //4.写内容
        ofs << "姓名:张三" << endl;
        ofs << "性别:男" << endl;
        ofs << "年龄:18" << endl;
        //5.关闭文件
        ofs.close();
    }

    int main() {

        test01();

        system("pause");
        return 0;
    }

    总结:

    * 文件操作必须包含头文件 fstream
    * 写文件可以利用 ofstream  ,或者fstream类
    * 打开文件时候需要指定操作文件的路径,以及打开方式
    * 利用<<可以向文件中写数据
    * 操作完毕,要关闭文件

    #include
    using namespace std;
    #include
    #include //头文件包含

    //文本文件  读文件
    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;
        while ((c = ifs.get()) != EOF)//EOF end of file文件尾部的标准
        {
            cout << c;
        }
        //5.关闭文件
        ifs.close();

    }

    int main() {

        test01();

        system("pause");

        return 0;
    }

    总结:

    - 读文件可以利用 ifstream  ,或者fstream类
    - 利用is_open函数可以判断文件是否打开成功
    - close 关闭文件

    #include
    using namespace std;
    #include
    #include //头文件包含

    //二进制文件  写文件
    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(p));

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

    int main()
    {

        test01();

        system("pause");

        return 0;
    }

    总结:

    * 文件输出流对象 可以通过write函数,以二进制方式写数据

    #include
    using namespace std;
    #include
    #include //头文件包含

    //二进制文件  读文件
    class Person
    {
    public:
        char m_Name[64];//姓名
        int m_Age;//年龄
    };

    void test01()
    {
        //1.包含头文件

        //2.创建流对象
        ifstream ifs("person.txt", ios::in | ios::binary);
        //3.打开文件  判断文件是否打开成功
        if (!ifs.is_open())
        {
            cout << "文件打开失败" << endl;
        }
        //4.读文件
        Person p;
        ifs.read((char *)&p, sizeof(p));
        cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
        //5.关闭文件
        ifs.close();
    }

    int main() {

        test01();

        system("pause");

        return 0;
    }

    总结- 文件输入流对象 可以通过read函数,以二进制方式读数据

  • 相关阅读:
    B+树索引(8)之索引适用场景(下)
    随笔(如何提高系统的响应速度)
    前后端分离 + 跨域 + 跨域的解决办法
    程序员的 100款代码表白特效,一个比一个浪漫
    设计师必备的插画设计素材网站
    释放搜索潜力:基于ES(ElasticSearch)打造高效的语义搜索系统,让信息尽在掌握[2.项目讲解篇],支持Linux/Windows部署安装
    基于web的医疗设备销售业务系统的设计与实现
    Certificates does not conform to algorithm constraints 解决方案
    海底电缆探测技术总结
    《Maven入门基础》
  • 原文地址:https://blog.csdn.net/AAAAAB111/article/details/136125101