流随机访问

17.39

复制代码
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;

int main()
{
    fstream inOut("copyOut",fstream::ate|fstream::in|fstream::out);
    if(!inOut)
    {
        cerr<<"Unable to open file!"<<endl;
        return EXIT_FAILURE;
    }
    auto end_mark=inOut.tellg();
    inOut.seekg(0,fstream::beg);
    size_t cnt=0;
    string line;
    while(inOut&&inOut.tellg()!=end_mark&&getline(inOut,line))
    {
        cnt+=line.size()+1;
        auto mark=inOut.tellg();
        inOut.seekp(0,fstream::end);
        inOut<<cnt;
        if(mark!=end_mark)
            inOut<<" ";
        inOut.seekg(mark);
    }
    inOut.seekp(0,fstream::end);
    inOut<<'\n';
    return 0;
}
复制代码

 

posted @   Jessica程序猿  阅读(320)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示