鱼C论坛

 找回密码
 立即注册
查看: 1119|回复: 27

[已解决]运行出错

[复制链接]
发表于 2020-10-11 15:20:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#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 findA11Codes(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 findA11Codes (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, "%5/%5", 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);
                    findA11Codes(thePath);
            findALLDirs(thePath);
                }
        }while(_findnext(handle, &fa) == 0);
       
        _findclose(handle);
}

int main()
{
        char path[MAX] = ".";
       
        printf("计算中...\n");
       
        findA11Codes(path);
        findALLDirs(path);
       
        printf("目前你总共写了 %1d 行代码! \n\n", total);
        system("pause");
       
        return 0;
}
编译显示can not open the file
结果为 目前打了0行代码
球球各位大神看一下
最佳答案
2020-10-11 15:51:08
3个地方有错误,27,51,97行,注释有写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-11 15:27:45 | 显示全部楼层
我运行你的程序,编译是正常的,没有报错,我就不知道你哪里写错了,你试一下我的代码:

  1. // 如果程序文件是 *.cpp,则将第 49 行的 "/*.c" 直接改为 "/*.cpp"

  2. #include <io.h>
  3. #include <direct.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>

  7. #define MAX        256

  8. long total;

  9. int countLines(const char *filename);
  10. void findAllCodes(const char *path);
  11. void findALLFiles(const char *path);

  12. int countLines(const char *filename)
  13. {
  14.     FILE *fp;
  15.     int count = 0;
  16.     int temp;

  17.     if ((fp = fopen(filename, "r")) == NULL)
  18.         {
  19.         fprintf(stderr, "Can not open the file: %s\n",filename);
  20.         return 0;
  21.         }

  22.     while ((temp = fgetc(fp))!= EOF)
  23.         {
  24.         if(temp == '\n')
  25.                 {
  26.             count++;
  27.                 }
  28.         }

  29.     fclose(fp);

  30.     return count;
  31. }

  32. void findAllCodes(const char *path)
  33. {
  34.     struct _finddata_t fa;
  35.     long handle;
  36.     char thePath[MAX], target[MAX];

  37.     strcpy(thePath,path);
  38.     if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)// 如果程序文件是 *.cpp,则将此行的 "/*.c" 直接改为 "/*.cpp"
  39.         {
  40.         do
  41.                 {
  42.             sprintf(target, "%s/%s", path, fa.name);
  43.             total += countLines(target);
  44.                 }
  45.         while (_findnext(handle, &fa) == 0);
  46.         }

  47.     _findclose(handle);
  48. }

  49. void findALLDirs(const char *path)
  50. {
  51.     struct _finddata_t fa;
  52.     long handle;
  53.     char thePath[MAX];

  54.     strcpy(thePath, path);
  55.     if((handle =  _findfirst(strcat(thePath, "/*"),&fa)) == -1L)
  56.         {
  57.         fprintf(stderr, "The path %s is wrong!\n",path);
  58.         return;
  59.         }

  60.     do
  61.         {
  62.         if(!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  63.             continue;

  64.         if( fa.attrib == _A_SUBDIR)
  65.                 {
  66.             sprintf(thePath, "%s/%s", path, fa.name);
  67.             findAllCodes(thePath);
  68.             findALLDirs(thePath);
  69.                 }
  70.         }
  71.     while (_findnext(handle, &fa) == 0);

  72.     _findclose(handle);
  73. }

  74. int main()
  75. {
  76.     char path[MAX] = ".";

  77.     printf("计算中...\n");

  78.     findAllCodes(path);    // C要大写
  79.     findALLDirs(path);

  80.     printf("目前你总共写了 %ld 行代码! \n\n", total);
  81.     system("pause");

  82.     return 0;
  83. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 15:32:11 | 显示全部楼层
乐乐学编程 发表于 2020-10-11 15:27
我运行你的程序,编译是正常的,没有报错,我就不知道你哪里写错了,你试一下我的代码:

你的就可以,我的显示0行代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 15:33:59 | 显示全部楼层
楠楠宝贝吖 发表于 2020-10-11 15:32
你的就可以,我的显示0行代码

肯定是你的代码哪里有写错,但软件没有报错,也就无法帮你查找了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 15:38:49 | 显示全部楼层
楠楠宝贝吖 发表于 2020-10-11 15:32
你的就可以,我的显示0行代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 15:40:32 | 显示全部楼层
乐乐学编程 发表于 2020-10-11 15:33
肯定是你的代码哪里有写错,但软件没有报错,也就无法帮你查找了

它会显示can not open the file
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 15:42:51 | 显示全部楼层
楠楠宝贝吖 发表于 2020-10-11 15:40
它会显示can not open the file


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 15:43:50 | 显示全部楼层
乐乐学编程 发表于 2020-10-11 15:33
肯定是你的代码哪里有写错,但软件没有报错,也就无法帮你查找了

能加个好友吗?我不太会用这个网站
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 15:50:18 | 显示全部楼层
  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX      256

  7. long total;

  8. int countLines(const char * filename);
  9. void findA11Codes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count = 0;
  15.         int temp;
  16.       
  17.         if((fp = fopen(filename, "r")) == NULL)
  18.         {
  19.                 fprintf(stderr, "Can not open the file :%s\n",filename);
  20.                 return 0;
  21.         }
  22.       
  23.         while ((temp = fgetc(fp)) != EOF)//括号打错地方
  24.         {
  25.                 if (temp == '\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.     }
  30.    
  31.     fclose(fp);
  32.    
  33.     return count;
  34. }

  35. void findA11Codes (const char *path)
  36. {
  37.         struct _finddata_t fa;
  38.         long handle;
  39.         char thePath[MAX],target[MAX];
  40.       
  41.         strcpy(thePath,path);
  42.         if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  43.         {
  44.                 do
  45.                 {
  46.                         sprintf(target, "%s/%s", path, fa.name);// 小写s不是不是数字5
  47.                         total +=countLines(target);
  48.                 }while (_findnext(handle, &fa) == 0);
  49.         }
  50.       
  51.         _findclose(handle);
  52. }

  53. void findALLDirs(const char *path)
  54. {
  55.         struct _finddata_t fa;
  56.         long handle;
  57.         char thePath[MAX];
  58.       
  59.         strcpy(thePath,path);
  60.         if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  61.         {
  62.                 fprintf(stderr, "The path %s is wrong!\n",path);
  63.                 return;
  64.         }
  65.       
  66.         do
  67.         {
  68.                 if(!strcmp(fa.name,".")|| !strcmp(fa.name,".."))
  69.                        continue;
  70.                        
  71.             if(fa.attrib ==_A_SUBDIR)
  72.             {
  73.                 sprintf(thePath,"%s/%s", path,fa.name);
  74.                 findA11Codes(thePath);
  75.                     findALLDirs(thePath);
  76.             }
  77.         }while(_findnext(handle, &fa) == 0);
  78.       
  79.         _findclose(handle);
  80. }

  81. int main()
  82. {
  83.         char path[MAX] = ".";
  84.       
  85.         printf("计算中...\n");
  86.       
  87.         findA11Codes(path);
  88.         findALLDirs(path);
  89.       
  90.         printf("目前你总共写了 %ld 行代码! \n\n", total);// 小写L不是数字1
  91.         system("pause");
  92.       
  93.         return 0;
  94. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 15:51:08 | 显示全部楼层    本楼为最佳答案   
3个地方有错误,27,51,97行,注释有写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-10-11 15:53:47 | 显示全部楼层
baige 发表于 2020-10-11 15:51
3个地方有错误,27,51,97行,注释有写

没有报错。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 15:55:00 | 显示全部楼层
baige 发表于 2020-10-11 15:51
3个地方有错误,27,51,97行,注释有写

您能说清楚一点吗、蟹蟹啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 16:02:05 | 显示全部楼层
楠楠宝贝吖 发表于 2020-10-11 15:32
你的就可以,我的显示0行代码

我发现了你的程序中下面几个本应是小写字母 l 的,你都写成了数字 1 :
void findA11Codes(const char *path);
void findA11Codes (const char *path)
findA11Codes(path);
printf("目前你总共写了 %1d 行代码! \n\n", total);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 16:12:45 | 显示全部楼层
我找出上面的问题都花了不少的时间,但程序仍然还有错误,不能正常运行。有鉴于此,我给你一个建议:这道题它的意义在于让你自己动动,练习敲键盘,而不是让你写这个程序,所以,不要再纠结这道题了,采纳我那个就好。知道C语言是怎么回事就行了,节约下来的时间,你可以多看几遍视频,多看几页书,甚至开始自己写个小程序什么的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 16:13:42 | 显示全部楼层
乐乐学编程 发表于 2020-10-11 16:02
我发现了你的程序中下面几个本应是小写字母 l 的,你都写成了数字 1 :
void findA11Codes(const char * ...

我都改过了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 16:15:47 | 显示全部楼层
乐乐学编程 发表于 2020-10-11 16:12
我找出上面的问题都花了不少的时间,但程序仍然还有错误,不能正常运行。有鉴于此,我给你一个建议:这道题 ...

好的,蟹蟹啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 16:18:50 | 显示全部楼层
本帖最后由 乐乐学编程 于 2020-10-11 16:20 编辑


加好友,没问题,你可能级别还不够,要够 100 积分,达到 鱼友I 这个级别才行。刚才我加你了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 16:24:38 | 显示全部楼层
乐乐学编程 发表于 2020-10-11 16:18
加好友,没问题,你可能级别还不够,要够 100 积分,达到 鱼友I 这个级别才行。刚才我加你了

我是说其他联系方式啥的,我刚加了这个,不太会用’
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 16:29:01 | 显示全部楼层
楠楠宝贝吖 发表于 2020-10-11 16:24
我是说其他联系方式啥的,我刚加了这个,不太会用’

QQ、微信,我都很少时间上,眼睛本来就不好了,再长时间看那么小的字,我怕我会瞎了。你要加,给你个QQ号,你加吧:626988698
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 16:30:39 | 显示全部楼层
挨审查了,什么时候能放出来,这可不知道了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 13:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表