我在我的程序中有下面的代碼。 IoFile.out只有幾行。爲什麼ostream的put指針被修改,即使我沒有使用ostream對象
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;
}
tellp和tellg的輸出如註釋中所示。 我的查詢是將文件內容寫入cout時我只希望streambuf的讀指針(即tellg)移動到文件末尾。但在這種情況下,我看到outStream的put指針也移動到文件結尾。爲什麼是這樣 ?爲什麼將文件打印到cout導致outStream的放置指針改變?
你打印整個文件,對吧? 您在ostream構造器中使用了一個streambuffer指針。 – Lefsler 2013-04-26 17:45:03