windowsϵͳΪʲôÎҵĵÚÒ»¿Î´úÂëÔËÐÐÖ®ºóÏÔʾ:ÎÒÒÑдÁË0ÐдúÂ룿²»Ó¦¸ÃÊÇ
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countlines(const char *filename);
void findAllCodes(const char *path);
void findALLCodes(const char *path);
int countLines(const char *filename)
{
FILE *fp;
int count = 0;
int temp;
if ((fp = fopen(filename,"r")) ==NULL)
{
fprintf(stderr,"Can not open the file:%s\n", filename);
return 0;
}
while ((temp = fgetc(fp)) != EOF)
{
if (temp == '\n')
{
count++;
}
}
fclose(fp);
return count;
}
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath, target;
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath,"/*.c"), &fa)) != -1L)
{
do
{
sprintf(target, "%s/%s", path, fa.name);
total += countLines(target);
}while (_findnext(handle, &fa) ==0);
}
_findclose(handle);
}
void findALLDirs(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath;
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath,"/*"), &fa)) == -1L)
{
fprintf(stderr, "The path %s is wrong!\n",path);
return;
}
do
{
if (!strcmp(fa.name,".") || !strcmp(fa.name,".."))
continue;
if(fa.attrib == _A_SUBDIR)
{
sprintf(thePath, "%s/%s", path, fa.name);
findAllCodes(thePath);
findALLDirs(thePath);
}
}while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path = ".";
printf("¼ÆËãÖÐ...\n");
findAllCodes(path);
findALLDirs(path);
printf("ÄãĿǰ×ܹ²Ð´ÁË %1d ÐдúÂ룡 \n\n", total);
system("pause");
return 0;
} #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 --) ;
if((k > 0 && k < m - 1) && (! stricmp(& fn , ".c") || ! stricmp(& fn , ".h") || ! stricmp(& fn , ".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 , wb ;
int m ;
strcpy(bf , path) ;
if(bf != '\\') 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 ;
total = 0 ;
printf("Input the search path : ") ;
scanf("%s" , wb) ;
if(strlen(wb) > 0) {
findall(wb) ;
printf(" Total source code lines : %d\n" , total) ;
}
} ÎÒÒ²ÏëÖªµÀЦ¿Þ ´úÂëû´íµÄ»°,¾Í¿´×Ô¼º³ÌÐòµÄºó׺Ãû,Èç¹ûÊÇcppµÄ»°¸ÄΪc ²âÊÔÁËһϴúÂ벢ûÓдí,ÄǾÍÓ¦¸ÃÊǺó׺ÃûµÄÎÊÌâÁË,°Ñ³ÌÐòµÄºó׺Ãû¸ÄΪ.c
»òÕß°Ñif ((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)¸ÄΪ
if ((handle = _findfirst(strcat(thePath, "/*.cpp"), &fa)) != -1L) baige ·¢±íÓÚ 2020-9-26 12:26
²âÊÔÁËһϴúÂ벢ûÓдí,ÄǾÍÓ¦¸ÃÊǺó׺ÃûµÄÎÊÌâÁË,°Ñ³ÌÐòµÄºó׺Ãû¸ÄΪ.c
»òÕ߰ѸÄΪ
¸Ðл´óÀÐȷʵÈç´Ë£¬²»¹ýÕâÊÇÎªÊ²Ã´ÄØ
baige ·¢±íÓÚ 2020-9-26 12:26
²âÊÔÁËһϴúÂ벢ûÓдí,ÄǾÍÓ¦¸ÃÊǺó׺ÃûµÄÎÊÌâÁË,°Ñ³ÌÐòµÄºó׺Ãû¸ÄΪ.c
»òÕ߰ѸÄΪ
ÍÛ£¡¸Ðл¸Ðл£¬ÎÒÒ²ÃÔ»óÁ˺þÃ
±¾Ìû×îºóÓÉ Éϰ¶µÄ½Û×Ó ÓÚ 2022-1-13 16:12 ±à¼
baige ·¢±íÓÚ 2020-9-26 12:26
²âÊÔÁËһϴúÂ벢ûÓдí,ÄǾÍÓ¦¸ÃÊǺó׺ÃûµÄÎÊÌâÁË,°Ñ³ÌÐòµÄºó׺Ãû¸ÄΪ.c
»òÕ߰ѸÄΪ
ΪʲôÎÒ¸ÄÁËÒÔºóÔËÐеĽá¹ûÊÇ
¼ÆËãÖÐ...
Can not open the file: ./hello world.cpp
Can not open the file: ./³.cpp
ĿǰÄã×ܹ²Ð´ÁË 0 ÐдúÂ룡
hello world.cppÊǽÌѧÊÓÆµ¸ú×ö£¬³.cpp¾ÍÊǿκó×÷Òµ
{:10_266:}
¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª
´òÈÅÁË ¶ÁÎļþµÄʱºò°Ñr´ò³ÉÁËf
Ò³:
[1]