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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 即时通讯聊天源码

    即时通讯聊天源码介绍:前端开发语言:VUE(安卓,IOS,WEB为一套前端代码)前端操作需要设置vue主程序的三个api接口发布h5打包上传服务器即可后台地址:域名/admin_loginTP伪静态,防跨域关掉网盘下载地址:http://kekewl.net/ShL1UQbsuts0图片:…

    2022年5月14日
  • nessus使用教程扫描_Nessus扫描IP无结果

    nessus使用教程扫描_Nessus扫描IP无结果转载:https://www.cnblogs.com/youcanch/articles/5671238.htmlNessus号称是世界上最流行的漏洞扫描程序,全世界有超过75000个组织在使用它。该工具提供完整的电脑漏洞扫描服务,并随时更新其漏洞数据库。Nessus不同于传统的漏洞扫描软件,Nessus可同时在本机或远端上遥控,进行系统的漏洞分析扫描。Nessus也是渗透测试重要工具之一。所…

    2022年10月19日
  • go int转string_map转list对象数组

    go int转string_map转list对象数组最近用go重构python项目。遇见一些问题,简单记录一下。1.string转map为什么要想到这个转换方式呢,主要是python项目中用到的是string转字典。比如:前端传过来的{“book”:”python基础教程”}。用python简单接收之后,用json.load很简单转为字典。用go的话,最简单的方式是string转map。class_detail_…

    2022年9月11日
  • python字符串模糊匹配_python匹配字符

    python字符串模糊匹配_python匹配字符广告关闭提供包括云服务器,云数据库在内的50+款云计算产品。打造一站式的云产品试用服务,助力开发者和企业零门槛上云。假设我有一个名为file1.txt的下面的文本文件:adammalejohnmalemikemalesuefemale我有下面的清单fullname=我希望能够通过文本文件,如果它有任何匹配,修改带有找到的单词的行,输出应如下所示:adammalejohnmalef…

    2022年7月25日
  • Config ConnectionStrings

    Config ConnectionStrings<connectionStrings><addname=”MyschoolConnectionString”connectionString=”DataSource=.;InitialCatalog=MySchool1;uid=sa;pwd=123456″/></connectionStrings>stringcon…

    2022年5月11日
  • win10修改视频默认播放器_win10无法更改默认播放器

    win10修改视频默认播放器_win10无法更改默认播放器你要用到WindowsPowerShell,它是win10系统自带的一个应用,要打开它,就单击开始菜单中的“所有应用”,然后找到WindowsPowerShell的文件夹,右键单击WindowsPowerShell(注意不是WindowsPowerShellISE),然后单击以管理员身份运行,就打开了。输入以下:get-appxpackage*zunevideo*|r

    2022年9月1日

发表回复

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

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