Change or check the openmode of a std::ofstream

心已入冬 提交于 2019-12-05 06:40:14

@Ari Since the default implementation doesn't allow what you want todo, you might have to encapsulate ofstream and provide the additional get/set open mode functionality in which your new object would simulate the desired behavior.

Maybe something like so

class FileOutput{
  private:
    ostream& streamOut;
    std::ios_base::openmode currentOpemMode;
  public:
    FileOutput(ostream& out, std::ios_base::openmode mode)
     : streamOut(out), currentOpemMode(mode){}

    void setOpenMode(const std::ios_base::openmode newOpenMode){
          if(newOpenMode != currentOpemMode){
              currentOpemMode = newOpenMode;
              updateUsedMode();
          }
    }
  private:
    void updateUsedMode(){
          if(currentOpemMode == ios_base::app){  /* use seekg/tellg to move pointer to end of file */}
          else if(currentOpenMode == binary){ /* close stream and reopen in binary mode*/}
         //...and so on
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!