前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >seekg的应用案例

seekg的应用案例

作者头像
全栈程序员站长
发布2022-08-31 17:00:43
3260
发布2022-08-31 17:00:43
举报

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

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

代码如下:

代码语言:javascript
复制
#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.cn/143055.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档