tellg和seekg_C ++ tellg(),seekg()和seekp()示例

这篇博客介绍了C++中用于读写文件时设置和获取get和put指针位置的函数tellg(), seekg()和seekp()。通过示例程序展示了如何使用这些函数,并提供了相关语法和常量说明。" 128752064,8628902,使用R语言创建正态分布图表指南,"['r语言', '开发语言', '数据分析', '概率统计']
摘要由CSDN通过智能技术生成

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

输出量

0
8
e
9
19
i
20
End:93
s

After the program execution the file content is,

程序执行后,文件内容为

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


翻译自: https://www.includehelp.com/cpp-programs/tellg-seekg-and-seekp.example.aspx

tellg和seekg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值