Fstream fails to create new file

前端 未结 6 1093
梦如初夏
梦如初夏 2021-01-12 22:57

I\'m using a FileManager for a project so that reading and writing is less of a hassle for me. Or would be, if I didn\'t spend all this time debugging it. So, this comfort-c

6条回答
  •  死守一世寂寞
    2021-01-12 23:28

    Just get your function to open

    void FileManager::open(std::string const& filename)
    {
        using std::ios_base;
        if(stream_.is_open())
            stream_.close();
    
        stream_.open(filename.c_str(), ios_base::in | ios_base::out | ios_base::trunc);
    }
    

    if that is the mode you require.

    There is no magic way to open a file for read/write creating it if it does not exist but not truncating it (removing its content) if it does. You have to do that in multiple steps.

提交回复
热议问题