seekp和tellp函数

WintelZhao 2002-12-27 01:17:29
作业需要,谁能解释一下seekp和tellp函数的用法,还有例子??
...全文
371 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
winco 2002-12-27
  • 打赏
  • 举报
回复
msdn上的解释:
ostream::seekp
ostream& seekp( streampos pos );

ostream& seekp( streamoff off, ios::seek_dir dir );

Parameters

pos

The new position value; streampos is a typedef equivalent to long.

off

The new offset value; streamoff is a typedef equivalent to long.

dir

The seek direction specified by the enumerated type ios::seek_dir, with values including:

ios::beg Seek from the beginning of the stream.


ios::cur Seek from the current position in the stream.


ios::end Seek from the end of the stream.
Remarks

Changes the position value for the stream. Not all derived classes of ostream need support positioning. For file streams, the position is the byte offset from the beginning of the file; for string streams, it is the byte offset from the beginning of the string.

ostream::tellp
streampos tellp();

Return Value

A streampos type that corresponds to a long.

Remarks

Gets the position value for the stream. Not all derived classes of ostream need support positioning. For file streams, the position is the byte offset from the beginning of the file; for string streams, it is the byte offset from the beginning of the string. Gets the value for the stream’s put pointer.

痞子酷 2002-12-27
  • 打赏
  • 举报
回复
fseek 和 ftell 倒还知道,是不是你想要的?
#include <stdio.h>

long filesize(FILE *stream);

int main(void)
{
FILE *stream;

stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
fclose(stream);
return 0;
}

long filesize(FILE *stream)
{
long curpos, length;

curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;

}

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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