计算代码行数的小程序

/******************************************
*   使用方法….在命令行下运行
*   lineamount.exe > 要输出行数的文件名
*   输入含有要统计的文件路径的文件
*
*******************************************/

#include "stdafx.h"
#include "stdio.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int countline( string filepath );

int _tmain(int argc, _TCHAR* argv[])
{
   string filepath;

   cin>>filepath;

   fstream filespath(filepath.c_str(),ios_base::in);
   if(!filespath){
       cout<<"读取文件失败!"<<endl;
   } else {
       int linecount = 0, temp = 0, line = 0;
       string sep;
       while( filespath >> filepath ){
           temp = countline(filepath);
           if (temp > 0){

               cout<<temp<<"ttt"<<filepath<<endl;
               line++;
               linecount += temp;
           } else {
               cout<<"读取文件:"<<filepath<<"出错"<<endl;
           }
       }
       cout<<"总行数:"<<linecount<<endl;
   }
//   cin>>filepath;
   return 0;
}

int countline( string filepath )
{
   fstream filespath(filepath.c_str(),ios_base::in);
   if(!filespath){
       return -1;
   }
   int linecount = 0;
   string temp = "";
   while( filespath >> temp ){
       linecount++;
   }
   return linecount;
}

Read: 1131

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注