我的程序中有以下代码.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指针改变?
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |