tellg和seekg

tellg() , seekg() and seekp() functions are used to set/get the position of get and put pointers in a file while reading and writing.

tellg() , seekg()和seekp()函数用于在读取和写入时设置/获取get和put指针在文件中的位置。

Syntaxes:

    // tellg()
    streampos tellg();
    // seekg()
    istream& seekg (streampos pos);
    istream& seekg (streamoff off, ios_base::seekdir way);
    // seekp()
    stream& seekp (streampos pos);
    ostream& seekp (streamoff off, ios_base::seekdir way);
 

Here,

  • pos – represents the new absolute position within the stream (from the beginning).

    pos –表示流中新的绝对位置(从头开始)。

  • off – represents the offset to seek.

    off –代表要寻找的偏移量。

  • pos – represents the following constants,

    pos –代表以下常数,

    • ios_base::beg / ios::beg – beginning of the stream.
    • ios_base :: beg / ios :: beg –流的开始。
    • ios_base::cur / ios::cur – current position in the stream.
    • ios_base :: cur / ios :: cur –流中的当前位置。
    • ios_base::end / ios::end – end the stream.
    • ios_base :: end / ios :: end –结束流。

C ++程序来演示tellg(),seekg()和seekp()的示例 (C++ program to demonstrate the example of tellg(), seekg() and seekp())

In the below program, we are using a file 'my.txt', the file contains the following text,

在下面的程序中,我们使用文件“ my.txt”,该文件包含以下文本,

File: my.txt

档案:my.txt

IncludeHelp is specially designed to provide help to students, 
working professionals and job seekers
 

Program:

#include <iostream>
#include <fstream>
using namespace std;
int main()
    fstream F;
    // opening a file in input and output mode
    F.open("my.txt", ios::in | ios::out);
    // getting current location
    cout << F.tellg() << endl;
    // seeing 8 bytes/characters
    F.seekg(8, ios::beg);
    // now, getting the current location
    cout << F.tellg() << endl;
    // extracting one character from current location
    char c = F.get();
    // printing the character
    cout << c << endl;
    // after getting the character,
    // getting current location
    cout << F.tellg() << endl;
    // now, seeking 10 more bytes/characters
    F.seekg(10, ios::cur);
    // now, getting current location
    cout << F.tellg() << endl;
    // again, extracing the one character from current location
    c = F.get();
    // printing the character
    cout << c << endl;
    // after getting the character,
    // getting current location
    cout << F.tellg() << endl;
    // again, seeking 7 bytes/characters from beginning 
    F.seekp(7, ios::beg);
    // writting a character 'Z' at current location
    F.put('Z');
    // now, seeking back 7 bytes/characters from the end
    F.seekg(-7, ios::end);
    // now, printing the current location
    cout << "End:" << F.tellg() << endl;
    // extracting one character from current location
    c = F.get();
    // printing the character
    cout << c << endl;
    // closing the file
    F.close();
    return 0;
 

Output

End:93

After the program execution the file content is,

程序执行后,文件内容为

IncludeZelp is specially designed to provide help to students, 
working professionals and job seekers
下面以输入流函数为例介绍用法:
seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。
对于第一个参数,可以是正负数值,正的表示向后偏移,负的表示向前偏移。而第二个参数可以是:
ios::beg:表示输入流的开始位置
ios::cur:表示输入流的当前位置
ios::end:表示输入流的结束位置
tellg()函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。
                                    要读取文件必须包含头文件,这里包含了C++读写文件的方法。  可以使用fstream类,这个类可以对文件进行读写操作。  1、打开文件。  打开文件可以有两种方式,第一种可以使用fstream类的构造函数。  fstream file(“test.dat”,ios_base::in|ios_base::out|ios_base::app);  另外一种方法就是使用open函数。  fstream f
                                    在使用文件流的过程中,涉及到两个函数用来移动读写位置,分别是 seekpseekgseekp 函数用于已经打开要进行输出的文件,而 seekg 函数则用于已经打开要进行输入的文件。可以将 “p” 理解为 “put”,将 “g” 理解为 “get”,这样理解当然是有根据的,因为 seekp 可用于将信息 put(放入)到文件中,而 seekg 则可用于从文件中 get(获取)信息。
函数原型:
ostream& seekp( streampos pos );
ostream& seekp( streamoff off, ios::seek_dir dir );
istream& seekg( streampos pos );
istream& seekg(.
                                    本文主要总结用C++的fstream、ifstream、ofstream方法读写文件,然后用seekg()、seekp()函数定位输入、输出文件指针位置,用tellg()、tellp()获取当前文件指针位置。
一、核心类和函数功能讲解
fstream:文件输入输出类。表示文件级输入输出流(字节流);
ifstream:文件输入类。表示从文件内容输入,也就是读文件;
ofstream:文件输...
                                    例如,假如我在进行文件读写,而要定位到当前位置的三个字符之前,则需调用FileHandle.seekg(-3). 但如果我是在写入一个文件,并且比如我要重写后5个字符的内容,我就必须往回跳转5个字符,因而,我应该使用FileHandle.seekp(-5) .所以当你再次调用get()函数的时候,它会返回再下一个字符,而非前面那个。nCount表示要略过的字符数量,而delimiter —— 与它的名称有着同样的含义:假如你想在文件末尾停下,则可使用EOF值传入,这样一来此函数就等同于seekg();