fpos 类
此类模板描述了一个对象,该对象可以存储还原任何流内的任意文件位置指示器所需的全部信息。 fpos<St>类的对象能够有效存储至少两个成员对象:
一是 streamoff 类型的字节偏移。
二是供 basic_filebuf 类的对象使用的
St
类型的转换状态,通常为mbstate_t
。
它还能存储 fpos_t
类型的任意文件位置,以供 basic_filebuf 类的对象使用。 但是,对于文件大小受限的环境,streamoff
和 fpos_t
有时可能互换使用。 对于不具有依赖于状态的编码的流的环境,实际上可能不会使用 mbstate_t
。 因此,所存储成员对象的数目可能会有所不同。
语法
template <class Statetype>
class fpos
参数
Statetype
状态信息。
构造函数
构造函数 | 说明 |
---|---|
fpos | 创建一个包含有关流中位置(偏移量)信息的对象。 |
成员函数
成员函数 | 说明 |
---|---|
seekpos | 仅由 C++ 标准库内部使用。 请勿从代码中调用此方法。 |
state | 设置或返回转换状态。 |
运算符
运算符 | 说明 |
---|---|
operator!= | 测试文件位置指示器是否不相等。 |
operator+ | 递增文件位置指示器。 |
operator+= | 递增文件位置指示器。 |
operator- | 递减文件位置指示器。 |
operator-= | 递减文件位置指示器。 |
operator== | 测试文件位置指示器是否相等。 |
operator streamoff | 将 fpos 类型的对象强制转换为 streamoff 类型的对象。 |
要求
标头:<ios>
命名空间: std
fpos::fpos
创建一个包含有关流中位置(偏移量)信息的对象。
fpos(streamoff _Off = 0);
fpos(Statetype _State, fpos_t _Filepos);
参数
_Off
进入流的偏移量。
_State
fpos
对象的起始状态。
_Filepos
进入流的偏移量。
备注
第一个构造函数存储相对于文件开头和初始转换状态的偏移量 _Off。 如果 _Off 为 -1,则生成的对象表示无效的流位置。
第二个构造函数存储零偏移和对象 _State。
fpos::operator!=
测试文件位置指示器是否不相等。
bool operator!=(const fpos<Statetype>& right) const;
参数
right
要与之比较的文件位置指示器。
返回值
如果文件位置指示器不相等,则为 true
,否则为 false
。
注解
成员函数返回 !(*this == right)
。
示例
// fpos_op_neq.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>
int main( )
{
using namespace std;
fpos<int> pos1, pos2;
ifstream file;
char c;
// Compare two fpos object
if ( pos1 != pos2 )
cout << "File position pos1 and pos2 are not equal" << endl;
else
cout << "File position pos1 and pos2 are equal" << endl;
file.open( "fpos_op_neq.txt" );
file.seekg( 0 ); // Goes to a zero-based position in the file
pos1 = file.tellg( );
file.get( c);
cout << c << endl;
// Increment pos1
pos1 += 1;
file.get( c );
cout << c << endl;
pos1 = file.tellg( ) - fpos<int>( 2);
file.seekg( pos1 );
file.get( c );
cout << c << endl;
// Increment pos1
pos1 = pos1 + fpos<int>( 1 );
file.get(c);
cout << c << endl;
pos1 -= fpos<int>( 2 );
file.seekg( pos1 );
file.get( c );
cout << c << endl;
file.close( );
}
fpos::operator+
递增文件位置指示器。
fpos<Statetype> operator+(streamoff _Off) const;
参数
_Off
要按其递增文件位置指示器的偏移量。
返回值
文件中的位置。
注解
成员函数返回 fpos(*this) +=_Off
。
示例
请参阅 operator!=,了解使用 operator+
的示例。
fpos::operator+=
递增文件位置指示器。
fpos<Statetype>& operator+=(streamoff _Off);
参数
_Off
要按其递增文件位置指示器的偏移量。
返回值
文件中的位置。
注解
成员函数将 _Off 添加到存储的偏移成员对象,然后返回 *this
。 使用文件时,结果仅对不具有状态依赖编码的二进制流有效。
示例
请参阅 operator!=,了解使用 operator+=
的示例。
fpos::operator-
递减文件位置指示器。
streamoff operator-(const fpos<Statetype>& right) const;
fpos<Statetype> operator-(streamoff _Off) const;
参数
right
文件位置。
_Off
流偏移量。
返回值
第一个成员函数返回 (streamoff)*this - (streamoff) right
。 第二个成员函数返回 fpos(*this) -= _Off
。
示例
请参阅 operator!=,了解使用 operator-
的示例。
fpos::operator-=
递减文件位置指示器。
fpos<Statetype>& operator-=(streamoff _Off);
参数
_Off
流偏移量。
返回值
成员函数返回 fpos(*this) -= _Off
。
备注
使用文件时,结果仅对不具有状态依赖编码的二进制流有效。
示例
请参阅 operator!=,了解使用 operator-=
的示例。
fpos::operator==
测试文件位置指示器是否相等。
bool operator==(const fpos<Statetype>& right) const;
参数
right
要与之比较的文件位置指示器。
返回值
如果文件位置指示器相等,则为 true
,否则为 false
。
备注
成员函数返回 (streamoff)*this == (streamoff)right
。
示例
请参阅 operator!=,了解使用 operator+=
的示例。
fpos::operator streamoff
将 fpos
类型的对象转换为 streamoff
类型的对象。
operator streamoff() const;
备注
成员函数返回存储的偏移成员对象和存储为 fpos_t
成员对象一部分的任何其他偏移量。
示例
// fpos_op_streampos.cpp
// compile with: /EHsc
#include <ios>
#include <iostream>
#include <fstream>
int main( )
{
using namespace std;
streamoff s;
ofstream file( "rdbuf.txt");
fpos<mbstate_t> f = file.tellp( );
// Is equivalent to ..
// streampos f = file.tellp( );
s = f;
cout << s << endl;
}
0
fpos::seekpos
此方法仅由 C++ 标准库内部使用。 请勿从代码中调用此方法。
fpos_t seekpos() const;
fpos::state
设置或返回转换状态。
Statetype state() const;
void state(Statetype _State);
参数
_State
新的转换状态。
返回值
转换状态。
注解
第一个成员函数返回存储在 St
成员对象中的值。 第二个成员函数将 _State 存储在 St
成员对象中。
示例
// fpos_state.cpp
// compile with: /EHsc
#include <ios>
#include <iostream>
#include <fstream>
int main() {
using namespace std;
streamoff s;
ifstream file( "fpos_state.txt" );
fpos<mbstate_t> f = file.tellg( );
char ch;
while ( !file.eof( ) )
file.get( ch );
s = f;
cout << f.state( ) << endl;
f.state( 9 );
cout << f.state( ) << endl;
}