检查std :: fstream是处于写入还是读取模式

Tom*_*Tom 5 c++ mfc

我需要检查std::fstream打开文件是否处于读取和/或写入模式.

到目前为止,我发现iosbase::openmode,但我认为我无法访问它.

还有其他方法吗?

Far*_*had 1

您应该为打开模式设置一个变量。检查这个代码:

    fstream PatternFile;
    ios_base::openmode currentOpenMode = ios_base::out;
    strFile.Format(_T("yourFile.txt"));
    PatternFile.open(strFile, currentOpenMode);
    if(PatternFile.is_open())
    {
        if(currentOpenMode  == ios_base::out)
        {
            // bingo
        }
    }
Run Code Online (Sandbox Code Playgroud)