seekg的应用案例

seekg的应用案例在学习C++文件流控制时(链接)我们知道C++有一个标准库fstream该库定义了三个数据类型ofstreamifstream和fstream在练习相应的案例时,seekg()函数掌握的不是很好,后经过多次尝试,可以正常调用了代码如下:#include<fstream>#include<iostream>usingnamespacestd;intmain(){chardata[100];////以写模式打开文件

大家好,又见面了,我是你们的朋友全栈君。

在学习C++文件流控制时(链接)我们知道C++有一个标准库fstream
该库定义了三个数据类型 ofstream ifstream 和 fstream
在练习相应的案例时,seekg() 函数掌握的不是很好,后经过多次尝试,可以正常调用了

代码如下:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{ 
   

    char data[100];

     以写模式打开文件
    //ofstream outfile;
    //outfile.open("new.out");

    //cout << "Writing to the file" << endl;
    //cout << "Enter your name: ";
    //cin.getline(data, 100);

     向文件写入用户输入的数据
    //outfile << data << endl;
    //cout << "Enter your name: ";
    //cin.getline(data, 100);

     向文件写入用户输入的数据
    //outfile << data << endl;
    //cout << "Enter your name: ";
    //cin.getline(data, 100);

     向文件写入用户输入的数据
    //outfile << data << endl;
    //cout << "Enter your name: ";
    //cin.getline(data, 100);

     向文件写入用户输入的数据
    //outfile << data << endl;
    //cout << "Enter your name: ";
    //cin.getline(data, 100);

     向文件写入用户输入的数据
    //outfile << data << endl;
    //cout << "Enter your name: ";
    //cin.getline(data, 100);

     向文件写入用户输入的数据
    //outfile << data << endl;




    //cout << "Enter your age: ";
    //cin >> data;
    //cin.ignore();

     再次向文件写入用户输入的数据
    //outfile << data << endl;

     关闭打开的文件
    //outfile.close();

    char ch;


    // 以读模式打开文件
    fstream infile;
    infile.open("new.out");

    cout << "Reading from the file" << endl;
    infile >> data;

    // 在屏幕上写入数据
    cout << data << endl;


    cout << infile.tellg() << endl;
    // 再次从文件读取数据,并显示它
    infile >> data;
    cout << data << endl;

    cout << "a line" << endl;

    cout << infile.tellg() << endl;

    infile.get(ch);
    cout << ch << endl;


    infile.seekg(0L, ios::cur);

    cout << infile.tellg() << endl;

    infile.get(ch);
    cout << ch << endl;



    6
    infile.seekg(-1L, ios::cur);

    cout << infile.tellg() << endl;

    infile.get(ch);
    cout << ch << endl;


    infile.seekg(-1L, ios::cur);

    cout << infile.tellg() << endl;

    infile.get(ch);
    cout << ch << endl;


    infile.seekg(-2L, ios::cur);

    cout << infile.tellg() << endl;

    infile.get(ch);
    cout << ch << endl;


    //cout << infile.rdbuf() << endl;

    cout << "a line" << endl;

    //1111
    infile.seekg(-3, ios::end);
    infile.get(ch);
    cout << ch << endl;




    //6
    infile.seekg(-8, ios::end);


    //infile.get(ch);
    //cout << ch << endl;

    cout << infile.rdbuf() ;




    //3
    infile.seekg(0, ios::beg);
    infile.get(ch);
    cout << ch << endl;

    cout << 'a line' << endl;




    // 关闭打开的文件
    infile.close();

    return 0;
}

这段代码前半段负责写入程序,后半段从文件中读取数据
需要注意以下几点:

  • 在读取文件时,实例化 fstream 和 ifstream 均可
  • 使用 infile.tellg() 追踪文件指针的位置
  • 使用 cout << infile.rdbuf() ; 输出指针所在处的整个单词
  • ios::cur 在当前指针位置处跳跃
  • ios::beg 从头开始跳跃
  • ios::end 从后往前遍历
  • 使用 ios::end 时,如果想向前遍历,需要输入负的步长
  • infile >> data; 整行输出
    关于 ios::cur 指针部分还是有点迷糊,可以先通过 infile.tellg() 考察指针移动情况,需要用到的时候再深入学习。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143055.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SSDP 协议_Smb协议

    SSDP 协议_Smb协议1.组播地址2.SSDP,简单服务发现技术组播地址为了让组播源和组播组成员进行通信,需要提供网络层组播地址,即IP组播地址。同时必须存在一种技术将IP组播地址映射为链路层的组播MAC地址。1.IP组播地址(1)       IPv4组播地址IANA(InternetAssignedNumbersAuthority,互联网编号分配委员会)将D类地址空间分配给IP

    2022年10月11日
    2
  • phpstrom2021.9激活码【2021最新】[通俗易懂]

    (phpstrom2021.9激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS3…

    2022年3月26日
    46
  • 数组和对象的深拷贝_Java对象深拷贝工具类

    数组和对象的深拷贝_Java对象深拷贝工具类functiondeepCopy(obj,cache){if(cache===void0)cache=[]if(obj===null||typeofobj!==’object’)returnobjvarhit=find(cache,function(c){debuggerreturn…

    2022年10月2日
    2
  • java高级语言程序设计_高级程序设计语言包括

    java高级语言程序设计_高级程序设计语言包括Java程序设计(高级及专题)- GUI

    2022年4月22日
    38
  • mac使用obs进行斗鱼直播无法录制内置声音

    mac使用obs进行斗鱼直播无法录制内置声音/////////////////2016/11/12/////////////////////////////////by xbw//////////////////// 需要soundflower,下载链接安装之后会重启电脑。斗鱼直播间–750240

    2022年5月12日
    51
  • redis 乐观锁_用了乐观锁还需要事物吗

    redis 乐观锁_用了乐观锁还需要事物吗文章目录GeospatialHyperloglogBitmapsRedis事务悲观锁和乐观锁JedisSpringboot继承RedisGeospatial存储地理位置的数据结构应用场景朋友的定位,附近的人,打车距离计算Geospatial底层使用的是Zset127.0.0.1:6379> geoadd city 116.23 40.22 beijing 添加一个数据127.0.0.1:6379> geoadd city 121.47 31.23 shanghai 118.77

    2022年8月8日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号