求教ofstream操作
当我以前写进个文件后 再次打开 会清空原先文件内容
而用ios::app方式打开后 虽然不会清空文件内容 却也无法定位文件 修改文件内容
ios::app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.
msdn也这样说 they are always appended to the end
有什么好办法解决呢。。
记得以前C语言没有这种情况。。
[解决办法]
#include <fstream>
#include <iostream>
using namespace std;
//using std::ofstream;
int main()
{
int size = 8;
ofstream outfile;
outfile.open("C:\\abc.txt",ofstream::app);
outfile.seekp(size,ofstream::beg);//size表示要移动的字节数,整数表示往后移,负数表示往前移,
//ofstream::beg表示从开始位置移,ofstream::cur表示从当前位置移,ofstream::end表示从结尾移
//streampos pos=outfile.tellp();
//cout<<pos<<endl;
//outfile.write("hello world!",pos);
outfile<<"hello world!";
outfile.close();
return 0;
}
为什么还是在后面输入呢