ifstream之seekg/tellg


声明:我个人特别讨厌:收费专栏、关注博主才可阅读等行为,推崇知识自由分享,推崇开源精神,呼吁你一起加入,大家共同成长进步!


在文件读写的时候,一般需要借助fstream来进行文件操作,常见的操作有seekg()和tellg(),但是这两个函数有一些需要注意的地方,如下:

主要参考:

https://stackoverflow.com/questions/20506771/get-file-size-with-ifstreamseekg-and-tellg

https://stackoverflow.com/questions/28823258/which-of-these-if-the-correct-way-to-use-seekg

https://stackoverflow.com/questions/11714973/istream-seekg-offsets-and-iosend

定义,参考cppreference
seekg: Sets input position indicator of the current associated streambuf object.中文的意思是:设置当前关联streambuf对象的输入位置指示器
tellg: Returns input position indicator of the current associated streambuf object.中文的意思是:返回当前关联streambuf对象的输入位置指示器

首先准备一个test.txt,每行15个字符,共45个字符

cpp 复制代码
ssssssssssssss
aaaaaaaaaaaaaa
dddddddddddddd

测试程序:

cpp 复制代码
#include <iostream>
#include "fstream"
using namespace std;

int main() {
    int size = 0;
    std::string fileName = "../test.txt";
    ifstream in(fileName.c_str(), ifstream::in | ifstream::binary);
    if(in)
    {
        in.seekg(0,ifstream::end);
        size = in.tellg();
        cout <<"********** size stream1*** =" << size << endl;  // ********** size stream1*** =44
        in.seekg(0,ios::end);
        size = in.tellg();
        cout <<"********** size stream2*** =" << size << endl; // ********** size stream2*** =44
        in.seekg(ios::end);
        size = in.tellg();
        cout <<"********** size stream3*** =" << size << endl; // ********** size stream3*** =2
        in.seekg(10,ios::end);
        size = in.tellg();
        cout <<"********** size stream4*** =" << size << endl; //  ********** size stream4*** =54
        in.seekg(-10,ios::end);
        size = in.tellg();
        cout <<"********** size stream5*** =" << size << endl; // ********** size stream5*** =34
        in.seekg(0,ios::beg);
        size = in.tellg();
        cout <<"********** size stream6*** =" << size << endl; // ********** size stream6*** =0
        in.seekg(ios::beg);
        size = in.tellg();
        cout <<"********** size stream7*** =" << size << endl; // ********** size stream7*** =0
        in.seekg(14);
        in.seekg(0, ios::end);
        size = in.tellg();
        cout <<"********** size stream8*** =" << size << endl; // ********** size stream8*** =44
        in.seekg(10);
        in.seekg(0, ios::cur);
        size = in.tellg();
        cout <<"********** size stream9*** =" << size << endl; // ********** size stream9*** =10
        in.seekg(ios::beg,ios::end);
        size = in.tellg();
        cout <<"********** size stream10*** =" << size << endl; // ********** size stream10*** =44
        in.seekg(ios::beg);
        in.seekg(ios::end);
        size = in.tellg();
        cout <<"********** size stream11*** =" << size << endl; // ********** size stream11*** =2
        in.seekg(ios::cur);
        size = in.tellg();
        cout <<"********** size stream12*** =" << size << endl; // ********** size stream12*** =1
    }
}

代码分析:

  1. seekg(),用来设置stream的文件指针位置,如in.seekg(14)就是将文件指针设置到14位置处(相对begin位置),这里相当于in.seekg(14, ios::beg),但是对于in.seekg(ios::end)却截然不同,如stream3输出为2,这是因为enum seekdir {beg, cur, end},所以当使用ios::curios::end且只有一个参数的时候,会将ios::cur隐式转换为1ios::end隐式转换为2,所以stream3输出2,stream12输出1,这告诉我们当使用beg, cur, end,为了避免错误,请使用两个参数,如in.seekg(0,ios::end)代替in.seekg(ios::end)
  2. tellg(),没啥好说的,就是返回文件流指针所在位置

声明:我个人特别讨厌:收费专栏、关注博主才可阅读等行为,推崇知识自由分享,推崇开源精神,呼吁你一起加入,大家共同成长进步!


相关推荐
凤年徐5 分钟前
【数据结构】时间复杂度和空间复杂度
c语言·数据结构·c++·笔记·算法
踏莎行hyx26 分钟前
使用langchain连接llama.cpp部署的本地deepseek大模型开发简单的LLM应用
c++·ai·langchain·大模型·llama.cpp·deepseek
山河木马34 分钟前
前端学C++可太简单了:双冒号 :: 操作符
前端·javascript·c++
乌萨奇也要立志学C++1 小时前
【C++详解】STL-list模拟实现(深度剖析list迭代器,类模板未实例化取嵌套类型问题)
c++·list
闻缺陷则喜何志丹2 小时前
【前缀和 BFS 并集查找】P3127 [USACO15OPEN] Trapped in the Haybales G|省选-
数据结构·c++·前缀和·宽度优先·洛谷·并集查找
序属秋秋秋3 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
十秒耿直拆包选手11 小时前
Qt:主窗体(QMainwindow)初始化注意事项
c++·qt
霖0012 小时前
C++学习笔记三
运维·开发语言·c++·笔记·学习·fpga开发
mit6.82412 小时前
[shad-PS4] Vulkan渲染器 | 着色器_重新编译器 | SPIR-V 格式
c++·游戏引擎·ps4
tan77º13 小时前
【Linux网络编程】Socket - TCP
linux·网络·c++·tcp/ip