1. 程式人生 > >第十六週專案一(2)-小玩檔案

第十六週專案一(2)-小玩檔案

問題及程式碼:

/*
*煙臺大學計算機與控制工程學院
*檔名稱:xiangmu1(2).cpp
*作    者:閆安
*完成日期:2016年6月24日
*版 本 號:codeblocks 16.01
*
*問題描述:下面程式的功能是統計文字檔案abc.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;
}

執行結果: