定义于头文件 <ostream>
template< class CharT, |
类模板 basic_ostream
提供字符流上的高层输出操作。受支持操作包含有格式输出(例如整数值)和无格式输出(例如生字符和字符数组)。此功能以 basic_streambuf
类所提供的接口实现,通过 basic_ios
基类访问。典型的实现中, basic_ostream
无非继承的数据成员。
寻位
返回输出位置指示器
std::basic_ostream<CharT,Traits>::tellp
pos_type tellp(); |
返回当前关联的 streambuf
对象的输出位置指示器。
表现为无格式输出函数 (UnformattedOutputFunction) (除了不实际进行输出)。在构造并检查 sentry 对象后, |
(C++11 起) |
若 fail()==true ,则返回 pos_type(-1) 。否则,返回 rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::out) 。
参数
(无)
返回
成功时为当前输出位置指示器,若出现失败则为 pos_type(-1) 。
调用示例
#include <iostream>
#include <sstream>
int main()
{
std::ostringstream s;
std::cout << s.tellp() << std::endl;
s << 'h';
std::cout << s.tellp() << std::endl;
s << "ello, world ";
std::cout << s.tellp() << std::endl;
s << 3.14 << std::endl;
std::cout << s.tellp() << std::endl << s.str();
return 0;
}
输出
设置输出位置指示器
std::basic_ostream<CharT,Traits>::seekp
basic_ostream& seekp( pos_type pos ); |
(1) | |
basic_ostream& seekp( off_type off, std::ios_base::seekdir dir ); |
(2) |
设置当前关联 streambuf
对象的输出位置指示器。
表现为无格式输出函数 (UnformattedOutputFunction) (除了不实际进行输出)。在构造并检查 sentry 对象后, |
(C++11 起) |
1) 通过调用 rdbuf()->pubseekpos(pos, std::ios_base::out) ,设置输出位置指示器为绝对(相对于文件起始)值 pos
。若调用返回 (pos_type)-1 ,则执行 setstate(failbit) 。
2) 通过调用 rdbuf()->pubseekoff(off, dir, std::ios_base::out) 设置输出位置指示器为相对于 dir
的偏移 off
。
不报告错误。 | (C++14 前) |
若调用返回 (pos_type)-1 ,则执行 setstate(failbit) 。 | (C++14 起) |
参数
pos | - | 要设置输出位置指示器到的绝对位置。 | ||||||||
off | - | 要设置输出位置指示器到的相对位置。 | ||||||||
dir | - | 定义应用相对偏移到的基位置。它能为下列常量之一:
|
返回值
*this
异常
1) 若 exceptions() & failbit != 0 ,则失败情况下可以抛出 std::ios_base::failure 。
2)
不抛出,除非 rdbuf()->pubseekoff() 抛出。 | (C++14 前) |
若 exceptions() & failbit != 0 ,则失败情况下可以抛出 std::ios_base::failure 。 | (C++14 起) |
调用示例
#include <sstream>
#include <iostream>
int main()
{
std::ostringstream os("hello, world");
os.seekp(7);
os << 'W';
os.seekp(0, std::ios_base::end);
os << '!';
os.seekp(0);
os << 'H';
std::cout << os.str() << '\n';
}