boost 文件内容读写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhdzxc123/article/details/79607027

    1使用boost::filesystem::fstream

    

#include <boost/filesystem.hpp>
using namespace boost::filesystem;
struct history{
    history(){}
    ~history(){}
    uint32_t m_id;
    uint32_t m_kk;
    uint32_t m_sec;
    std::string name;
    friend fstream& operator >> (fstream & f, history &h){
        char c;
        f >> h.m_id;
        f >> c;
        f >> h.m_kk;
        f >> c;
        f >> h.m_sec;
        f >> c;
        f >> h.name;
        f >> c;
    }

    friend fstream& operator << (fstream & f, const history &h){
        f << h.m_id;
        f << ",";
        f << h.m_kk;
        f << ",";
        f << h.m_sec;
        f << ",";
        f << h.name;
        f << "\n";
    }

};


int main() {

    std::string path = "wwww1.txt";

    history history1;
    history1.m_id = 20;
    history1.m_kk = 20;
    history1.m_sec = 20;
    history1.name = "abacdefasdfasdfwerwer";

    //file path
    boost::filesystem::path fpath(path);
    boost::filesystem::fstream fstream(fpath, std::ios_base::out);

    //write data to file
    fstream << history1;
    fstream.close();


    boost::filesystem::fstream fstream1(fpath, std::ios_base::in);
    history history2;
    //read data from file
    fstream1 >> history2;

    std::cout << history2.m_id << std::endl;
    std::cout << history2.name << std::endl;


    fstream1.close();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhdzxc123/article/details/79607027