为什么 tellp() return 0 对于 ios::app 而不是 ios::ate?

Why does tellp() return 0 for ios::app but not for ios::ate?

我有以下代码片段:

ofile.open("New1.dat",ios::app|ios::binary|ios::ate);
long bytes = ofile.tellp()/sizeof(t);
cout<<ofile.tellp()<<endl;    //line 1
t.input(bytes);
ofile.write((char *)&t,sizeof(t));
ofile.close();

当我删除 ios::appios::ate 时,line 1 的输出为 0,但是只有当它们都在一起时,它们才会给出正确的文件位置。为什么会这样? P.S。我知道 ios::app 和 ios::ate 之间的区别。

提前致谢!

来自 std::ios::openmode(C++11 的第 27.5.3.1.4 节)

std::ios::app 表示每次写入前都查找到流的末尾。因此流可能不会在 结束之前 任何写入操作。无论指针在哪里(0 或其他地方)写入总是在最后完成。 (隐式寻道在每次写操作前结束)

另一方面 std::ios::ate 意味着在打开后立即寻找到流的末尾,因此保证 return 文件大小。

进一步阅读:C++ Filehandling: Difference between ios:app and ios:ate?