C++实现对本地文件加行号并输出到本地文件

#include <fstream>
#include <strstream>
using namespace std;

int main(int argc,char*argv[])
{
    strstream textfile;
    ifstream in(argv[1]);
    textfile << in.rdbuf();
    in.close();

    ofstream out(argv[2]);

    const int bsz = 100;                             //每次每行最多读取的字符数
    char buf[bsz];
    int line = 0;
    while(textfile.getline(buf,bsz/*,'\n'*/))        //默认的终止符是回车
    {
        out.setf(ios::right,ios::adjustfield);
        out.width(1);                                //不写也可
        out << ++ line << ". " <<buf<<endl;
    }
    out.close();
    return 0;
}

posted @ 2021-03-13 12:35  Weisswire  阅读(280)  评论(0编辑  收藏  举报