首页 新闻 会员 周边

计算代码行数的代码,为什么结果是0行?大神们看看那里出错了

0
[待解决问题]

为啥计算不出来,显示的是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 findAllFiles(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[MAX], target[MAX];
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[MAX];
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[MAX] = ".";
printf("计算中...\n");
findAllCodes(path);
findALLDirs(path);
printf("目前你总共写了 %1d 行代码!\n\n", total);
system("pause");
return 0;
}

七九灰的主页 七九灰 | 菜鸟二级 | 园豆:202
提问于:2020-02-21 16:15

建议改进一下排版,支持 markdown 语法

dudu 4年前

@dudu: 这样看得清楚点了吗?编辑时没看到可以选代码格式的选项,不知道怎么操作,你可以帮我看看这个代码那里有问题吗?感谢。

七九灰 4年前
< >
分享
所有回答(3)
0

先给找到的代码文件的path打印出来看看

jakio6 | 园豆:1318 (小虾三级) | 2020-02-21 19:33
0

这你要考虑到 windows系统里, 是 \r\n的

风中的雪糕 | 园豆:418 (菜鸟二级) | 2020-02-21 20:12
0

给你个思路,读取文件大小判断你的代码数量,还有还有这有何意义?????编辑器插件也有这种功能

小小咸鱼YwY | 园豆:3210 (老鸟四级) | 2020-02-22 20:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册