windows下统计某个目录下的源代码的行数

C++代码行数统计

 

 1 #include <stdio.h>
 2 #include <windows.h>
 3 
 4 using namespace std;
 5 
 6 int countLines(char * src){
 7   int count = 0;
 8   WIN32_FIND_DATA findFileData;
 9   char * toFind = new char[200];
10   strcpy(toFind,src);
11   strcat(toFind,"\\*.*");
12   HANDLE hFind = FindFirstFile(toFind,&findFileData);
13   if (hFind == INVALID_HANDLE_VALUE)
14     return count;
15   while(TRUE)
16     {
17       if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
18     if(findFileData.cFileName[0]!='.') { //目录,并且不是隐藏目录
19       char * tmpPath = new char[200];
20       strcpy(tmpPath,src);
21       strcat(tmpPath,"\\");
22       strcat(tmpPath,findFileData.cFileName);
23       count += countLines(tmpPath);
24       delete []tmpPath;
25     }
26       }
27       else if (strstr(findFileData.cFileName,".as")!=NULL ||strstr(findFileData.cFileName,".mxml")!=NULL){ //这是我筛选出源代码文件的条件
28     char * filePath = new char[200];
29     char buffer[1024];
30     strcpy(filePath,src);
31     strcat(filePath,"\\");
32     strcat(filePath,findFileData.cFileName);
33     FILE *fp = fopen(filePath,"r"); 
34     int line = 0;
35     while (fgets(buffer,1024,fp)){
36       line++;
37     }
38     fclose(fp);
39     count += line;
40     printf("%s: %d\n",findFileData.cFileName,line);
41       }
42       if(!FindNextFile(hFind,&findFileData))
43     break;
44     }
45   return count;
46 }
47 int main(int argc, char* argv[]){
48   int lineCount = 0,i;
49   for (i=1; i<argc; i++){
50     lineCount += countLines(argv[i]);
51   }
52   printf("Total Num of Lines: %d\n",lineCount);
53   return 0;
54 }

 

 

 

转载于:https://www.cnblogs.com/zhanghs/archive/2013/03/15/2962360.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值