fstream 的文件指针问题

Arcticanimal 2010-11-23 10:15:39
文件以追加方式打开时,若文件已经存在,为什么tellp返回的仍然是0?
eg.

std::ofstream fs;
fs.open("out.txt", std::ios::out|std::ios::app);
fs << "hello world.";
fs.close();

fs.open("out.txt", std::ios::out|std::ios::app);
// fs << "glad to see you."; // 毫无疑问这个写操作会写到hello world之后
if( static_cast<size_t>(fs.tellp()) )
{
cout << "out.txt already exists.\n";
}
fs.close();


HP-UX 或 Win32+VS2008 环境下,上面的代码对文件写指针的测试都是失败的。
又测试了一下Win32 的 CreateFile 函数,用OPEN_ALWAYS打开文件,然后SetFilePointer获取文件指针,得到的文件指针位置也是0,这个倒可以理解,CreateFile不区分读写指针。

难道“tellp指向哪里下一次的写操作就写到哪里”这个规则不成立??那么tellp有什么意义呢?
求解.
...全文
353 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
psdty 2012-10-25
  • 打赏
  • 举报
回复
谢谢分享
半斗 2010-11-23
  • 打赏
  • 举报
回复
使用ios::app模式,新内容只会写到文件的后面,所以seekp、tellp没有起作用,但使用seekp后用tellp返回的位置是正确的。
在if( static_cast<size_t>(fs.tellp()) )前加上fs.seekp(0,ios::end);即可
要用tellp就别用app模式
Arcticanimal 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lizhong2613 的回复:]
C/C++ code
template<class _E, class _Tr> inline
basic_ostream<_E, _Tr>& __cdecl operator<<(
basic_ostream<_E, _Tr>& _O, const _E *_X)
{typedef basic_ostream<_E, _Tr> _Myos;……
[/Quote]

我测试的结果是,seekp操作貌似无效,以追加方式打开的文件每一次写都会写到文件尾
而tellp返回值很不靠谱


ofstream testfs;
testfs.open("test.txt", ios::out|ios::trunc|ios::binary);
testfs.write("12345", 5);
testfs.close();

testfs.open("test.txt", ios::out|ios::app|ios::binary);
cout << "initial open: " << static_cast<size_t>(testfs.tellp()) << '\n';
testfs.write("6789", 4);
cout << "after write: " << static_cast<size_t>(testfs.tellp()) << '\n';
testfs.seekp(2, ios::beg);
cout << "after seek beg: " << static_cast<size_t>(testfs.tellp()) << '\n';
testfs.write("haha", 4);
cout << "write again: " << static_cast<size_t>(testfs.tellp()) << '\n';
testfs.close();


上面的代码输出是这样的:
initial open: 0
after write: 9
after seek beg: 2
write again: 13

最后test.txt的内容是:
123456789haha
lizhong2613 2010-11-23
  • 打赏
  • 举报
回复
template<class _E, class _Tr> inline
basic_ostream<_E, _Tr>& __cdecl operator<<(
basic_ostream<_E, _Tr>& _O, const _E *_X)
{typedef basic_ostream<_E, _Tr> _Myos;
ios_base::iostate _St = ios_base::goodbit;
size_t _N = _Tr::length(_X);
size_t _M = _O.width() <= 0 || _O.width() <= _N
? 0 : _O.width() - _N;
const _Myos::sentry _Ok(_O);
if (!_Ok)
_St |= ios_base::badbit;
else
{_TRY_IO_BEGIN
if ((_O.flags() & ios_base::adjustfield)
!= ios_base::left)
for (; 0 < _M; --_M)
if (_Tr::eq_int_type(_Tr::eof(),
_O.rdbuf()->sputc(_O.fill())))
{_St |= ios_base::badbit;
break; }
if (_St == ios_base::goodbit
&& _O.rdbuf()->sputn(_X, _N) != _N)
_St |= ios_base::badbit;
if (_St == ios_base::goodbit)
for (; 0 < _M; --_M)
if (_Tr::eq_int_type(_Tr::eof(),
_O.rdbuf()->sputc(_O.fill())))
{_St |= ios_base::badbit;
break; }
_O.width(0);
_CATCH_IO_(_O) }
_O.setstate(_St);
return (_O); }

个人感觉他在第一次处理之前没有设置读写指针所在位置,当有输入操作时,才读取位置最后重置读写位置,所以你直接读取是不行的,你可以试试先写入一个字符,然后seekp(-1),这时再读取应该就行了。

不好意思,我试了一下seekp()那种方法也不行,之后也成零了,谁能读懂那个源码,分析分析???
龙哥依旧 2010-11-23
  • 打赏
  • 举报
回复

// basic_ostream_seekp.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>

int main()
{
using namespace std;
ofstream x("basic_ostream_seekp.txt");
streamoff i = x.tellp();
cout << i << endl;
x << "testing";
i = x.tellp();
cout << i << endl;
x.seekp(2); // Put char in third char position in file
x << " ";

x.seekp(2, ios::end); // Put char two after end of file
x << "z";
}
ryfdizuo 2010-11-23
  • 打赏
  • 举报
回复
www.cplusplus.com
上面看看例子吧~
Arcticanimal 2010-11-23
  • 打赏
  • 举报
回复
看的文档都是说tellp返回下一次写的位置,看来也是陷阱一个
不管怎么说我觉得用ios::app方式打开文件,初始的tellp应该返回文件尾的位置
一直还在用tellp计算文件长度来着,要注意了。。。

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧