鱼C论坛

 找回密码
 立即注册
查看: 2096|回复: 8

[已解决]S1E2运行结果为0

[复制链接]
发表于 2021-10-23 21:09:32 | 显示全部楼层 |阅读模式

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

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

x
S1E2的课后作业
我用Dev c++照着把代码敲完了,

  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 findALLCodes(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.        
  24.         while((temp = fgetc(fp)) != EOF)
  25.         {
  26.                 if(temp == '\n')
  27.                 {
  28.                         count++;
  29.                 }
  30.          }
  31.          
  32.          fclose(fp);
  33.          
  34.          return count;
  35. }

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

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

  83. int main()
  84. {
  85.         char path[MAX] = ".";
  86.        
  87.         printf("计算中...\n");
  88.        
  89.         findALLCodes(path);
  90.         findALLDirs(path);
  91.        
  92.         printf("目前你总共写了 %ld 行代码!\n\n",total);
  93.         system("pause");
  94.        
  95.         return 0;
  96. }
复制代码
运行结果却是0,检查了好几遍没有错误,然后把代码直接复制进去了试一下结果也是0,自己也不懂怎么找出问题,求助各位大佬了
最佳答案
2021-10-23 21:36:09
第二个错误也是一样的
  1. sprintf(thePath, "%s/%s", path, fa.name);
复制代码

你在那两个函数找一找就能发现了
  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 findALLCodes(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.         
  24.         while((temp = fgetc(fp)) != EOF)
  25.         {
  26.                 if(temp == '\n')
  27.                 {
  28.                         count++;
  29.                 }
  30.          }
  31.          
  32.          fclose(fp);
  33.          
  34.          return count;
  35. }

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

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

  85. int main()
  86. {
  87.         char path[MAX] = ".";
  88.         
  89.         printf("计算中...\n");
  90.         
  91.         findALLCodes(path);
  92.         findALLDirs(path);
  93.         
  94.         printf("目前你总共写了 %ld 行代码!\n\n",total);
  95.         system("pause");
  96.         
  97.         return 0;
  98. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-23 21:29:18 | 显示全部楼层
然后把代码直接复制进去了试一下结果也是0
就冲这句话,你的源代码文件后缀是 .cpp 吧?改成 .c
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-23 21:33:04 | 显示全部楼层
  1. sprintf(target,"%s%s",path, fa.name);
复制代码

原句
  1. sprintf(target,"%s/%s",path, fa.name);
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-23 21:36:09 | 显示全部楼层    本楼为最佳答案   
第二个错误也是一样的
  1. sprintf(thePath, "%s/%s", path, fa.name);
复制代码

你在那两个函数找一找就能发现了
  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 findALLCodes(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.         
  24.         while((temp = fgetc(fp)) != EOF)
  25.         {
  26.                 if(temp == '\n')
  27.                 {
  28.                         count++;
  29.                 }
  30.          }
  31.          
  32.          fclose(fp);
  33.          
  34.          return count;
  35. }

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

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

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

使用道具 举报

发表于 2021-10-23 21:37:38 | 显示全部楼层
注意后缀要为,c
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-25 20:25:09 | 显示全部楼层

谢谢,改了一下可以正确运行了,虽然还不懂原理就是
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-25 20:26:31 | 显示全部楼层
大马强 发表于 2021-10-23 21:36
第二个错误也是一样的

你在那两个函数找一找就能发现了

谢谢,可以了,改完这些再把文件后辍改了.c运行就没问题了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-25 20:27:17 | 显示全部楼层
大马强 发表于 2021-10-23 21:36
第二个错误也是一样的

你在那两个函数找一找就能发现了

谢谢,可以了,改完这些再把文件后辍改了.c运行就没问题了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-25 20:30:11 | 显示全部楼层
与时俱学进 发表于 2021-10-25 20:27
谢谢,可以了,改完这些再把文件后辍改了.c运行就没问题了

刚入站,回复的都是同一个人才发现,sorry
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 13:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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