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


相关推荐

  • com QueryInterface「建议收藏」

    com QueryInterface「建议收藏」客户同组件的交互都是通过一个接口完成的。在客户查询组件的其他接口时,也是通过接口完成的。这个接口就是IUnknown。它在UNKNWN.H头文件定义 :如下

    2022年7月22日
    11
  • CCS 8.00 软件中视窗的应用

    1.多种视窗通过CCS界面View可以看到存在多种视窗;memorybrowser在调试中可以查看SARAM中对应地址的数值;Register:DSP各存储模块的变化(类似系统关键字);Expressions和Variables是运用最多的,方便看程序中定义的变量。Disasembly方便查看C语言和汇编语言对应关系;Breakpoint方便对断点进行管理。2.断点管理断点管理试图:可以单一或者批量删除断点;屏蔽断点;启动断点需要在复选框中打钩。3.变量变化无论是regis

    2022年4月9日
    48
  • golang 时间轮_窗口时间

    golang 时间轮_窗口时间timewheelGolang实现的时间轮,项目地址安装goget-ugithub.com/ouqiang/timewheel使用packagemainimport(“github.com/ouqiang/timewheel””time””fmt”)funcmain(){//tick刻度为1秒,3600个槽,执行的jobtw

    2022年9月26日
    3
  • python程序设计实践题EXP01-求圆面积、温度转换和绘制五角星

    python程序设计实践题EXP01-求圆面积、温度转换和绘制五角星一、计算圆的面积思路:根据圆面积的计算公式进行求解。程序代码:1importmath2radius=253area=math.pi*radius**2#**是幂运算4p

    2022年7月5日
    25
  • 一个简单的ETL开发的过程(informatica)

    一个简单的ETL开发的过程(informatica)大致的了解过程,中间不涉及组件部分。 正文PowerCenter的开发过程大致可以分为几步:1.在客户端PowerCenterDesigner中导入源表和目标表的结构定义。(只是表结构)2.在PowerCenterDesigner中执行的事件为:   1&gt;.创建Mapping。   2&gt;.拖动源和目标进入Mapping。(类似于定义变量)   …

    2022年5月29日
    102
  • 国外推荐:计算机专业人士必读的书籍_计算机专业排名世界

    国外推荐:计算机专业人士必读的书籍_计算机专业排名世界国外大牛推荐:计算机专业人士必读好书(30本经典)分类:程序人生2014-04-1123:17175人阅读评论(0)收藏举报计算机书籍1.《代码大全》史蒂夫·迈克康奈尔  推荐数:1684  “优秀的编程实践的百科全书,《代码大全》注重个人技术,其中所有东西加起来,就是我们本能所说的“编写整洁的代码”。这本书有50

    2022年9月25日
    2

发表回复

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

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