|
发表于 2023-2-24 21:22:56
|
显示全部楼层
本楼为最佳答案
本帖最后由 ExiaGN001 于 2023-2-25 15:18 编辑
首先强调一下发帖格式问题:
2. 代码大于20行以上时,最好写清自己的思路和注释,这样回答的人才能尽快给大家答案!(避免扔上一大堆代码,说是有错误,请人指点!)
3. 发代码请务必使用编辑器的“添加代码文字”(这个符号:<>)
有用请设置最佳答案谢谢
代码问题如下(斜体红字为编译器报错信息(翻译过),斜体蓝字为运行逻辑的问题,蓝字为C语言预留关键字,绿字为C++ STL库):
第13行 没有viod类型(您的意思是"void"吗?)
把viod改成void
第17行 没有FILEn类型(您的意思是"FILE"吗?)
把FILEn 改成FILE (注意大小写,这里要表扬把*写在靠变量一侧的好习惯)
第18行 在int前缺少','或';' (报错显示是19行,但错误在第18行)
行尾漏加分号(一定是英文分号,初学者常犯错误)
第40行 没有viod类型(您的意思是"void"吗?)
viod改成void
第42行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
需要用空格把struct 和_finddata_t 隔开
第51行 sprintf字符串的输出不对
改成这样:sprintf(target,"%s\\%s",path,fa.name);
第61行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
需要用空格把struct 和_finddata_t 隔开
第79行 sprintf的参数不对
改成这样:sprintf(thePath,"%s\\%s" ,path,fa.name);
第90行 没有定义过path(您的意思是"patj"吗?)(报错显示是第94行)
把90行的patj改成path即可。
第51行和第79行的输出其实不对,在C/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[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;
- }
复制代码
有用请设置最佳答案谢谢
|
|