想要實作這樣的一個功能:
在檔案的指定位置寫入資料,如果原來那個位置有資料了,替換、插入都可以。
在檔案中順序寫入資料
在檔案中寫資料時,不清除原檔案內容,而是在檔案中直接寫入資料
請問,有沒有這些功能的函式啊?
ofstream ofs("test.txt",ios::binary);//比如這個,只能順序寫入資料,還會清除原檔案內容
ofs.write((char*)&a,sizeof(int));
ofs.write((char*)&a,sizeof(int));
ofs.close();
uj5u.com熱心網友回復:
CFile::Seekvirtual LONG Seek(LONG lOff,UINT nFrom);
throw(CFileException);
回傳值:
如果要求的位置合法,則Seek回傳從檔案開始起的新位元組偏移量。否則值未定義并產生CFileException例外。
引數: lOff 指標移動的位元組數。
nFrom 指標移動的模式,可為以下值之一: · CFile::begin 從檔案開始,把指標向后移動lOff位元組。
· CFile::current 從當前位置開始,把指標向后移動lOff位元組。
· CFile::end 從檔案尾開始,把指標向前移動lOff位元組。注意必須移動到存在的檔案中,因而lOff應為負。如果為正值,則超出檔案尾。
uj5u.com熱心網友回復:
在寫之前加上這句:ofs.seekp(offset, ios::beg); // 定位到開頭offset位元組處
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/285164.html
標籤:基礎類
上一篇:致CSDN論壇用戶
下一篇:MFC CString 賦值觸發 First-chance exception at 0x634f81b0 (mfc90ud.dll) in XXX.exe