为什么ostream的put指针被修改,即使我没有使用ostream对象

ira*_*ppa -1 c++

我的程序中有以下代码.IoFile.out几行.

    int main()
    {
     ifstream inFile("Iofile.out", ios::in|ios::out);
     ostream outStream(inFile.rdbuf());

     cout << "tellp outStream " << outStream.tellp() << endl;  //  tellp outStream 0
     cout << "tellg inFile " << inFile.tellg() << endl;        //  tellg inFile 0

     cout << inFile.rdbuf();  // Print whole file

     cout << "tellp outStream " << outStream.tellp() << endl;  //  tellp outStream 21
     cout << "tellg inFile " << inFile.tellg() << endl;        //  tellg inFile 21
     return 0;    
}
Run Code Online (Sandbox Code Playgroud)

tellp和tellg的输出如注释中所示.我的查询是当我将文件内容写入cout时,我希望只有streambuf的读指针(即tellg)移动到文件的末尾.但在这种情况下,我看到outStream的put指针也被移动到文件末尾.为什么是这样 ?为什么要将文件打印到cout导致outStream的put指针改变?

Rei*_*ica 6

你无法打开ifstream写作.如果您想要读写访问权限,请使用fstream.