• C++ 核心编程 文件操作


     

    #include <iostream>
    using namespace std;
    #include <fstream>
    //读文件的操作函数
    void read() {
        //1、包含读写文件的头文件 #include <fostream>
        //2、创建读文件的对象
        ofstream fos;
        //3、指定以什么样的方式去打开一个文件
        fos.open("test.txt",ios::out);
        //4、写内容
        fos << "姓名:宫健丽" << endl;
        fos << "性格:活泼开朗" << endl;
        //5、关闭文件
    
    }
    int main() {
        read();
    
        system("pause");
    
        return 0;
    
    }

    #include <iostream>
    using namespace std;
    #include <fstream>
    #include <string>
    
    
    
    void write() {
        //1、包含头文件
        //2、创建流对象
        ifstream ifs;
        //3、以哪种方式读文件,并判断是否正常打开
        ifs.open("test.txt", ios::in);
    
        if (!ifs.is_open()) {
    
            cout << "文件打开方式有错" << endl;
    
        }
        //4、读文件
        //分别是4种打开方式
        //第一种
        //char buf[1024] = { 0 };//用数组buf去接收,{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 )) {
            cout << c;
        }
    
        //5、关闭文件
        ifs.close();
    
    
    }
    int main() {
        
        //char buf[1024] = {0};//{0} 是初始化字符数组
        write();
    
        system("pause");
        return 0;
    }

    #include <iostream>
    using  namespace std;
    #include <fstream>
    //二进制文件  写文件
    
    
    class Person
    {
    public:
            char m_Name[64];
            int m_Age;
    };
    //二进制写
    void test01() {
        ofstream ofs;
        ofs.open("person.txt", ios::out | ios::binary);
        Person p = { "张三",18 };
        ofs.write((const char*)&p,sizeof(Person));
        ofs.close();
    
    }
    
    //二进制读
    int main() {
    
        test01();
        system("pause");
        return 0;
    }

    #include <iostream>
    using  namespace std;
    #include <fstream>
    //二进制文件  写文件
    
    
    class Person
    {
    public:
            char m_Name[64];
            int m_Age;
    };
    //二进制写
    void test01() {
        ofstream ofs;
        ofs.open("person.txt", ios::out | ios::binary);
        Person p = { "张三",18 };
        ofs.write((const char*)&p,sizeof(Person));
        ofs.close();
    
    }
    
    //二进制读
    void test02() {
    
        ifstream ifs;
        ifs.open("person.txt", 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();
    }
    int main() {
    
        /*test01();*/
        test02();
        system("pause");
        return 0;
    }
  • 相关阅读:
    "Automation 服务器不能创建对象" 的解决方法
    让DataGrid拥有单击回传事件并带回指定字段的值
    SQL 和TSQL学习(一)
    数据写入XML
    DATALIST分页存储过程
    The target assembly contains no service types. You may need to adjust the Code Access Security policy of this assembly." 目标程序集不包含服务类型。可能需要调整此程序集的代码访问
    字符串转日期
    C# 日期和时间正则表达式
    js根据输入日期显示该月的最后一天的日期[转]
    JavaScript试题【解析+答案】
  • 原文地址:https://www.cnblogs.com/gjianli/p/15292492.html
Copyright © 2020-2023  润新知