#include <fstream>
#include <iostream>
int main()
{
const char* fileName = "out1";
std::ofstream fs1(fileName);
fs1 << "AAAAAAAAAAA\n";
std::cout << fs1.tellp() << std::endl;
fs1.close();
std::ofstream fs2(fileName, std::ios_base::ate);
std::cout << fs2.tellp() << std::endl;
fs2.close();
return 0;
}
gcc版本为4.4.6 20120305(Red Hat 4.4.6-4)(GCC)
g++ file02.cpp
./a.out
12 0
为什么fs2.tellp()输出0而不是12?
ate
而不是out
,则应该失败,因为您没有指定与标准中的fopen
模式字符串对应的模式标志的有效组合;您需要(至少)out | ate
,但您可能想要像"r+"
这样的东西,它是in | out
,您还可以选择添加ate
。只有out | ate
将被截断,因为它等同于"w"
。 - CB Bailey