是否未在追加模式下定义 tellp()
whether tellp() not defined in append mode?
我在C++中尝试使用文件指针。在下面的代码中,获得的结果是 0、30、10 和 12。所以这意味着如果我们确实 seekp((,tellp(( 在追加模式下不会给出正确的结果。我期待 tellp(( 在 seekp(( 和附加数据后给我 32。我知道在应用程序模式下,写作总是到最后,因此有这个疑问。结果是否意味着 tellp(( 位置在追加模式下无关紧要?
h1.txt 文件的内容是 : 01234567891011121314151617181911 并且符合预期。
ofstream f1("h1.txt",ios::app|ios::out);
if (!f1)
{
cerr<<"cannot open the filen";
exit(-1);
}
currentPos = f1.tellp();
cout<<"Initial Write Pointer Position = "<<currentPos<<endl;
// Write some data at the end
for(int index=0;index<20;index++)
{
f1<<index;
}
currentPos= f1.tellp();
cout<<"Write Pointer Position after write = "<<currentPos<<endl;
f1.seekp(10);
currentPos= f1.tellp();
cout<<"Write Pointer Position after seek = "<<currentPos<<endl;
/** Note: Even if you reposition pointer, in app mode, data will always be written to the end */
f1<<11;
currentPos= f1.tellp();
/** seekp does not match. In app mode, data is writtten to end */
cout<<"Final Write Pointer Position after seek and write = "<<currentPos<<endl;
f1.close();
Visual
C++ 2015 编译的应用打印 0、30、10、32。
可能是您拥有C++标准库版本中的错误。您使用什么编译器?
相关文章:
- 这个c++代码是如何在没有定义函数的情况下运行的
- A类的定义需要B类的定义,如何在不公开B的定义的情况下公开A的公共函数?
- 是否可以在不使用 decltype 的情况下推断先前定义的 extern 变量的类型
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- 用户定义的文本运算符(在原始模式下)存在问题
- 如何在不给它任何类型(如整数)的情况下定义某物?
- 如何声明具有在声明下定义的类型的参数的函数?
- 在 C++ 中,我可以在不修改类的情况下定义对类的隐式转换吗?
- 是否未在追加模式下定义 tellp()
- 在模板类定义下定义内联函数
- 如何在没有参数列表的情况下定义向量无效使用模板名称 std::vector
- 为什么您可以在 switch 语句中的"默认"下定义变量,而不能在"case"下定义变量
- 为什么给纯虚拟函数下定义是有意义的
- 在C++中,是否存在在特定条件下定义/运行宏的问题
- 为什么我不能在没有常量的情况下定义比较
- 为什么我不能在这个条件下定义变量
- c++在不知道对象类型的情况下定义成员函数指针
- 实现仅在特定条件下定义的类型定义
- c++如何在不知道确切参数的情况下定义函数
- c++在switch语句中定义变量-在初始化/不初始化的情况下定义