流随机访问
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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步