我正在使用fstream
對象,我在想如何在文件末尾寫入內容,而不必手動填充文件末尾的空間eof和所需位置爲零?如何在文件末尾的特定地址寫入
例子:
fstream someFile;
someFile.open("foo.dat", ios :: out | ios :: trunc | ios :: binary); // file is created, and empty
someFile.seekp(100, ios :: beg); // expecting 100 zeroes to be written in file, but nothing happens
someFile.write("yodelay-hee-hoo", 15); // file is still empty, does not work
我一直在尋找在這個問題上了大約一個小時,但我找不到任何多的相關。我遇到的唯一情況是有人說使用.seekp
與ios :: end
應該填補零差距,但我試過了,它不適合我。
如果有幫助,我在VS2010,本機編譯。謝謝你,如果你可以提供小費。
你說你想使用'ios :: end',但你的代碼說'ios :: beg' –
所以,seekp可能會將流設置爲錯誤狀態。什麼是錯誤? – hyde
爲什麼你不想自己寫這些零?它可能會有效,並會產生更爲明確的代碼。或者我錯過了什麼? – ereOn