新人求问第二节课动动手
我认真对着小甲鱼给的代码敲了,开始总是报错,我检查修改了几遍后,终于不报错了,可是程序却不能实现计数的功能:
图片:
我写的代码:
#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, 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;
}
求大佬指点,作为回报,我将献出我微不足道的鱼币! 看了一下就是你这个缩进有一点问题,帮你稍微改了一改,还有记得在电脑上让这个文件后缀名是.c
#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, 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("目前你总共写了 %ld 行代码!\n\n", total);
system("pause");
return 0;
} 复制你的代码
void findAllCodes(const char * path)
....
if((handle = _findfirst(strcat(thePath, "/*.c") , &fa)) != -1L) //用的 英文 全角,改成用 英文半角 输入就可以了,
void findALLDirs(const char *path)
...
if((handle = _findfirst(strcat(thePath,"/*"), &fa)) == -1L)//同上
我改完之后就输出正常 了
将文件夹下的. cpp后缀改为.c 即可
看过各位大佬宝贵的建议后,我又重写了一遍count_line,结果出现代码行数虚高的现象:
我附了一张图(不知道附成功了没,因为我只看到了一串代码emmmmm)
图片上显示编译执行后我的FishC里有6119行代码,可实际上我的代码只有333行
琢磨了半天,实在发现不了,只能求助大佬们了
我的代码是这样的:
#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,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("目前你总共写了 %d 行代码!\n\n",total);
system("pause");
return 0;
}
大佬们有发现什么吗?
嗯嗯,要是我的帖中没有附带图片还请大佬们教我一下如何附上图片;有的话就通知我一下,我是有附上图片的
蟹蟹啦,嘿! LuLD 发表于 2020-11-11 09:52
复制你的代码
void findAllCodes(const char * path)
嘿,大佬你这图片是咋搞出来的,是贴图吗 LuLD 发表于 2020-11-11 09:52
复制你的代码
void findAllCodes(const char * path)
还有联机搜索那是啥玩意,感觉挺好使的 似花颜 发表于 2020-11-11 19:56
嘿,大佬你这图片是咋搞出来的,是贴图吗
嗯,贴图
似花颜 发表于 2020-11-11 19:58
还有联机搜索那是啥玩意,感觉挺好使的
我用的 是 VS2019 版本的,鼠标放在写的函数啊,变量什么上面就会有出来解释的,联机搜索就是在网络上搜索这个函数的解释的网页之类的,就跟在百度里搜索问题答案 的 意思 似花颜 发表于 2020-11-11 19:54
看过各位大佬宝贵的建议后,我又重写了一遍count_line,结果出现代码行数虚高的现象:
我附了一张图( ...
while((temp = fgetc(fp))!= EOF)
{
if(temp = '\n') // if(判断条件 ) if(temp == '\n'),你这样写temp = '\n' 成 赋值了
{
count++;
}
}
代码 添加
打开后将代码贴进去 我不要你的鱼币,只要给个最佳答案就行。出去旅游多日,我已经满后很多了
// 有些编译器对缩进有严格的要求,所以,请不要改变每一行前的空格!
// 如果程序文件是 *.cpp,则将第 50 行的 "/*.c" 直接改为 "/*.cpp"
#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, target;
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)// 如果程序文件是 *.cpp,将此行的 "/*.c" 直接改为 "/*.cpp"
{
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;
}
页:
[1]