|  | 
 
 发表于 2019-8-10 15:21:32
|
显示全部楼层 
| 楼主再抄这个代码看看呢: 
 复制代码#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total = 0 ;
bool filter(const char * fn)
{
        bool ret                                                                                                                       ;
        int k , m                                                                                                                      ;
        ret = false                                                                                                                    ;
        m = strlen(fn)                                                                                                                 ;
        for(k = m - 1 ; k > 0 && fn[k] != '.' ; k --)                                                                                  ;
        if((k > 0 && k < m - 1) && (! stricmp(& fn[k] , ".c") || ! stricmp(& fn[k] , ".h") || ! stricmp(& fn[k] , ".cpp"))) ret = true ;
        return ret                                                                                                                     ;
}
int countLines(const char * filename)
{
        FILE * fp                                                ;
        int count = 0                                            ;
        if((fp = fopen(filename , "r")) != NULL) {
                while(! feof(fp)) if(fgetc(fp) == '\n') count ++ ;
                fclose(fp)                                       ;
        } else {
                count = - 1                                      ;
        }
        printf("%6d | %s\n" , count , filename)                  ;
        return count                                             ;
}
void findall(const char * path)
{
        struct _finddata_t fa                                                                       ;
        long handle                                                                                 ;
        char bf[MAX] , wb[MAX]                                                                      ;
        int m                                                                                       ;
        strcpy(bf , path)                                                                           ;
        if(bf[strlen(bf) - 1] != '\\') strcat(bf , "\")                                            ;
        strcpy(wb , bf)                                                                             ;
        strcat(wb , "*.*")     ;
        if((handle = _findfirst(wb , & fa)) != -1L) {
                strcpy(wb , bf)                                                                     ;
                strcat(wb , fa . name)                                                              ;                
                if (fa . attrib & _A_SUBDIR) {
                        if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) findall(wb)         ;
                } else {
                        if(filter(wb) && ((m = countLines(wb)) > 0)) total += m                     ;
                }
                while(! _findnext(handle , & fa)) {
                        strcpy(wb , bf)                                                             ;
                        strcat(wb , fa . name)                                                      ;                
                        if (fa . attrib & _A_SUBDIR) {
                                if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) findall(wb) ;
                        } else {
                               if(filter(wb) && ((m = countLines(wb)) > 0)) total += m              ;                      
                        }
                }
                _findclose(handle)                                                                  ;
        }                        
}
int main(void)
{
        char wb[MAX]                                                 ;
        total = 0                                                    ;
        printf("Input the search path [exit]: ")                     ;
        scanf("%s" , wb)                                             ;
        if(strlen(wb) > 0) {
                findall(wb)                                          ;
                printf("    Total source code lines : %d\n" , total) ;
        }
}
 | 
 |