为什么用输入,输出,追加模式在c ++中打开文件会出错?

时间:2014-04-09 11:59:47

标签: c++ file ofstream

#include <iostream>
#include <fstream>

int main()
{
    using namespace std;

    /* Open a file and read it*/

    ofstream file_obj;

    file_obj.open("file1.txt",ios::in| ios::out | ios::app);

    if( file_obj.is_open() )
    {
       printf("\n File opened successfully ");
    }
    else
    {
       printf("\n Error occured in opening the file");
    }

    return 0;
 }

它提供输出

打开文件时出错。

但当我删除ios时:工作正常。

为什么这种组合不可能?

1 个答案:

答案 0 :(得分:0)

ofstream类仅用于输出(写入文件) - 因此为“o”。使用fstream代替 - 这允许输入和输出。