小甲鱼C语言第一课课后作业设计的程序无法统计代码行数
一开始打完很开心,可是很快就发现它一直显示0行,我已经完成了好几课的作业了仍然是零,求助,怎么才能让他正确统计行数(我把这个程序和其他的代码都放一个文件夹里,但是仍然没用){:5_107:} 本帖最后由 迷雾少年 于 2019-8-16 11:16 编辑
JackII 发表于 2019-8-16 11:00
求大佬再帮我分析一波
你代码统计的是.C文件而你那个文件夹里的是 .Cpp文件
1.把代码里的.C改成.Cpp就统计.Cpp文件源代码行数了
if((handle = _findfirst(strcat(thePath,"/*.c"),&fa)) !=-1L)
改
if((handle = _findfirst(strcat(thePath,"/*.cpp"),&fa)) !=-1L)
2.把你那个文件夹里的.CPP文件改成.C文件,依然统计.C源代码行数
你自己写的代码有问题的话你也不贴代码,谁知道你咋回事哦 Krant5 发表于 2019-8-15 17:25
你自己写的代码有问题的话你也不贴代码,谁知道你咋回事哦
我是直接复制粘贴的答案 晚点我把它贴出来 现在回答问题成本越来越高了,要拿到ip再hack进主机再拿到源码才能回答{:10_266:}{:10_266:}{:10_266:}
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long toal;
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,target;
strcpy(thePath,path);
if((handle = _findfirst(strcat(thePath,"/*.c"),&fa)) !=-1L)
{
do
{
sprintf(target, "%s/%s",path, fa.name);
toal += 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("¼ÆËãÖD...\n");
findAllCodes(path);
findALLDirs(path);
printf("ĿǰÄã×ü12D′áË %ld DD′úÂë!\n\n",toal);
system("pause");
return 0;
}
来吧,大佬们这是你们要的代码 迷雾少年 发表于 2019-8-15 21:17
现在回答问题成本越来越高了,要拿到ip再hack进主机再拿到源码才能回答
男孩子,头像犯规 JackII 发表于 2019-8-15 21:23
男孩子,头像犯规
直接copy你的代码单独测了一下代码没问题,问题估计就是在这一行
char path = ".";
首先
.代表当前目录
..代表上一级目录
其次你要知道编译器给你的设置的程序运行目录具体在哪里
首先你单独设置一个绝对路径例如D:/test
然后在这个文件夹里放一个源代码
在代码修改
char path = "D:/test";
运行估计没问题
就是这个路径问题 JackII 发表于 2019-8-15 21:23
男孩子,头像犯规
忘了说了,我是男孩子,头像是女票{:10_250:} 第一张图片是我直接运行原来程序的图,第二张是我直接粘贴dalao的代码的图,第三张是在D盘创了个test文件后把原来的文件都搬到了test中后运行经过修改过的程序。我是不是操作错了T T{:9_239:} 迷雾少年 发表于 2019-8-15 22:39
忘了说了,我是男孩子,头像是女票
求大佬再帮我分析一波 迷雾少年 发表于 2019-8-16 11:14
你代码统计的是.C文件而你那个文件夹里的是 .Cpp文件
1.把代码里的.C改成.Cpp就统计.Cpp文件源代码 ...
搞定了,多谢dalao{:9_227:} JackII 发表于 2019-8-16 16:40
搞定了,多谢dalao
okok,结贴吧{:10_297:} 不错解决了
页:
[1]