Hi Li Qihong,
Do this... ------------------------------ #include <iostream> #include <fstream>
using std::fstream; using std::ios_base;
int main() { fstream f; f.open("1", ios_base::in | ios_base::out | ios_base::ate); // f.seekp(0, ios_base::end); -- unnecessary w/ios_base::ate f << "Haha"; f.close(); } ---------------------------------
You didn't specify the ios_base::in, which will prevent the ios_base::out from performing an ios_base::trunc for you.
Alternatively, you could do...
f.open("1", ios_base::out | ios_base::app);
...the ios_base::ate is redundant.
Please see p639 of Stroustrup's C++ Programming Language (special edition).
HTH, --Eljay