我正在尋找關於seekg()
和seekp()
如何寫入文件的說明。例如說我有一個文件,像這樣:fstream seekg(),seekp()和write()
offset 0: 2
offset 4: 4
offset 8: 6
offset 12: 8
offset 16: 10
現在我要打開的文件,並做一些試圖讀取和寫入值。
fstream file;
file.open("file.txt", fstream::in |fstream::out | fstream::binary);
file.seekp(0, ios::end) // seek to the end of the file
int eofOffset = file.tellp(); // store the offset of the end-of-file, in this case 20
int key = 0;
file.seekg(12, ios::beg); // set the seek cursor to offset 12 from the beginning of the file
file.read((char *) &key, (int)sizeof(int)); // read in the next 4 bytes and assign it to key, in this case 8
file.seekg(8, ios::beg); // set the seek cursor to offset 8 from the beginning of the file
file.read((char *) &key, (int)sizeof(int)); // read in the next 4 bytes and assign it to key, in this case 6
現在我想寫入文件的末尾。由於seekg()
函數只能移動查找光標,我的seekp()
光標應該仍然在文件的最後?所以:
int newKey = 12;
file.write((char *) &newKey, sizeof(int));
應該讓我的文件現在看起來像:
offset 0: 2
offset 4: 4
offset 8: 6
offset 12: 8
offset 16: 10
offset 20: 12
現在,如果我選擇尋求偏移,寫它的值作爲偏移量,這是價值會發生什麼我的文件剛剛插入。例如,我想offset 8
保持eofOffset = 20
,因爲我們剛剛在該偏移處插入了12。
如果我做的:
file.seekp(8, ios::beg);
file.write((char *) &eofOffset, sizeof(int));
它正確地重寫我的文件看起來像這樣:
offset 0: 2
offset 4: 4
offset 8: 20
offset 12: 8
offset 16: 10
offset 20: 12
請讓我知道如果我提出用seekg()
和seekp()
功能的任何錯誤。
謝謝!那麼你什麼時候會推薦刷新緩衝區?你會在每次讀寫之後刷新它,或者如果你正在進行一系列讀取或一系列寫入,它會保持原樣嗎? – raphnguyen 2013-03-28 18:50:10
@raphnguyen刷新緩衝區是一個相對昂貴的操作,通常不需要手動刷新,除非您正在從寫入切換到讀取,並且您不尋找,除非您有充足的理由刷新讓實現處理其他刷新。 – user657267 2013-03-28 22:25:24