seekg
描述 (Description)
它用于设置输入序列中的位置。
声明 (Declaration)
以下是std :: basic_istream :: seekg的声明。
(1) basic_istream& seekg (pos_type pos);
(2) basic_istream& seekg (off_type off, ios_base::seekdir way);
参数 (Parameters)
pos - 流中的新绝对位置(相对于开头)。
off - 偏移值,相对于way参数。
way - 类型为ios_base :: seekdir的对象。
返回值 (Return Value)
返回basic_istream对象(* this)。
异常 (Exceptions)
Basic guarantee - 如果抛出异常,则对象处于有效状态。
数据竞争 (Data races)
修改流对象。
例子 (Example)
在下面的示例中为std :: basic_istream :: seekg。
#include <iostream>
#include <fstream>
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
is.read (buffer,length);
is.close();
std::cout.write (buffer,length);
delete[] buffer;
}
return 0;
}