1. 程式人生 > >第十四周專案1——(2)

第十四周專案1——(2)

/*
*Copyright (c) 2014,煙臺大學計算機學院
*All rights reserved.
*檔名稱:main.cpp
*作者:蘇強
*完成日期:2015年6月1日
*版本號:v1.0
*
*問題描述:下面程式的功能是將文字檔案abc.txt中的所有行加上行號後寫到newabc.txt檔案中,請填空將程式補充完整。
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
    fstream outfile,infile;
    infile.open("abc.txt",ios::in); // (1)
    if(!infile)
    {
        cout<<"Can’t open the file."<<endl;
        abort();
    }
    outfile.open("newabc.txt",ios::out);//(2)
    if(!outfile)
    {
        cout<<"Can’t open the file."<<endl;
        abort();
    }
    char buf[80];
    int i=1;
    while(!infile.eof()) // (3)
    {
        infile.getline(buf,80); // (4)
        outfile<<i++<<": "<<buf<<endl; //(5)
    }
    infile.close();
    outfile.close();
    return 0;
}