鱼C论坛

 找回密码
 立即注册
查看: 1686|回复: 3

输出结果不对

[复制链接]
发表于 2023-9-10 18:10:18 | 显示全部楼层
你提供的代码是一个用于统计指定目录下所有C代码文件的行数的程序。在输出结果不正确的情况下,我注意到在findALLDirs()函数中可能存在问题。

请尝试使用以下修改后的代码:

  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. void findALLDirs(const char *path);

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

  31. void findAllCodes(const char *path) {
  32.     struct _finddata_t fa;
  33.     long handle;
  34.     char thePath[MAX];
  35.     char target[MAX];
  36.    
  37.     strcpy(thePath, path);
  38.    
  39.     if ((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L) {
  40.         do {
  41.             sprintf(target, "%s/%s", path, fa.name); // 修改此行
  42.             
  43.             total += countLines(target);
  44.         } while (_findnext(handle, &fa) == 0);
  45.     }
  46.    
  47.     _findclose(handle);
  48. }

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

  76. int main() {
  77.     char path[MAX] = ".";
  78.    
  79.     printf("计算中...\n");
  80.    
  81.     findAllCodes(path);
  82.     findALLDirs(path);
  83.    
  84.     printf("目前你总共写了 %ld 行代码!\n\n", total);
  85.     system("pause");
  86.    
  87.     return 0;
  88. }
复制代码


这些修改包括:
· 在findAllCodes()函数中,修复了路径拼接时可能出现的问题。
· 在findALLDirs()函数中,修复了路径拼接时可能出现的问题。

这些修改应该能够使程序正常工作并正确统计代码行数。如果还有其他问题,请提供更具体的描述。
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-25 03:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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