seekg的应用案例

在学习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() 考察指针移动情况,需要用到的时候再深入学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值