|
发表于 2019-3-7 00:50:44
|
显示全部楼层
#include<io.h>
#include<direct.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 256
总数;
int countLines(const char * filename);
void findAllCodes(const char * path);
void findAllFiles(const char * path);
int countiLines(const char * filename)
{
FILE * fp;
int count = 0;
int temp;
if ((fp = fopen_s(filename, "r") ) == NULL)
{
fprintf(stderr, "无法打开文件:%s\n", filename);
return 0;
}
while ((temp = fgetc(fp)) != EOF)
{
if (temp == '\n')
{
总数++;
}
}
FCLOSE(fp);
return 总数;
}
void findAllCodes(const char * path)
{
struct _finddata_t fa;
int handle;
char thePath[MAX], 目标[MAX],总计[MAX];
strcpy_s(thePath, path);
if ((handle = _findfirst(strcat_s(thePath, "\*。c"), &fa)) != -1L)
{
{
sprintf_s(目标,"%s/%s",path,fa.name);
总计+= countLines(目标);
}while (_findnext(handle, &fa) == 0);
}
_findclose(handle);
}
void findAllDirs(const char* path)
{
struct _finddata_t fa;
int handle;
char thePath[MAX];
strcpy_s(thePath, path);
if ((handle = _findfirst(strcat_s(thePath, "/ *"), &fa)) == -1L)
{
fprintf(stderr, "路径%s错了!\n", thePath);
return;
}
{
if (!strcmp(fa.name,"。") || !strcmp(fa.name,".."))
if (fa.attrib == _A_SUBDIR)
{
sprintf_s(thePath, "%s/%s", path, fa.name);
findAllCodes(thePath);
findAllDirs(thePath);
}
}while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path[MAX] = "。";
的printf("计算中...\n");
findAllCodes(path);
findAllDirs(path);
printf("目前你总共写了%d行代码!\n\n", 总计);
system("pause");
return 0;
} |
|