在 C++ 语言文件处理中使用 seekg() 设置位置
seekg()是iostream库中的一个函数,它允许我们在文件中查找任意位置。它主要用于设置在C++文件处理中从给定文件的输入流中提取的下一个字符的位置。
语法
istream&seekg(streamoff offset, ios_base::seekdir dir); istream&seekg(streampos position); Where, position: It is the new position in the stream buffer. offset: It is an integer value of type streamoff which represents the offset in the stream’s buffer, relative to the dir parameter. dir: It is the seeking direction. It is an object of type ios_base::seekdir that can take any offset constant value. Three direction used for offset values are: ios_base::beg = from the beginning of the stream’s buffer. ios_base::cur = from the current position in the stream’s buffer. ios_base::end = from the end of the stream’s buffer.
所需的步骤和操作
Begin Open a data file for input/output operations. Add ‘nhooo’ to the file. Seek to 9 position from beginning of the file. Read the next 5 characters from the file into buffer F. End F with null character as terminator character. Print the contents. Close the file. End
示例
#include#include using namespace std; int main (int argc, char** argv) { fstream File("d.txt", ios::in | ios::out ); File << "nhooo"; File.seekg(9, ios::beg); char F[9]; File.read(F, 5); F[5] = 0; cout < 输出结果 Point
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下