鱼C论坛

 找回密码
 立即注册
查看: 182|回复: 2

为什么我报的是写了0行,没有给我报错

[复制链接]
发表于 2025-3-13 16:59:48 | 显示全部楼层 |阅读模式

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

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

x
#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];
        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);
        printf("目前你总共写了 %1d 行代码! \n\n", total);
        system("pause");
        return 0;
       
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-3-13 17:00:13 | 显示全部楼层
你的代码有几个问题,导致它没有正确计算代码行数,以下是主要问题和修正建议:

### **1. `char thepath, target;` 变量定义错误**
在 `findAllCodes` 和 `findAllDirs` 函数中,你定义了 `char thepath, target;`,但 `thepath` 和 `target` 应该是字符串数组,而不是单个 `char` 变量。正确的定义方式如下:
  1. char thepath[MAX], target[MAX];
复制代码


### **2. `sprintf(target, "%s/%s", path, fa.name);` 可能导致缓冲区溢出**
`target` 需要足够的空间来存储路径,建议使用 `snprintf` 以避免缓冲区溢出:
  1. snprintf(target, MAX, "%s/%s", path, fa.name);
复制代码


### **3. `char path = ".";` 变量定义错误**
在 `main` 函数中,你定义了 `char path = ".";`,但 `path` 应该是字符串数组:
  1. char path[MAX] = ".";
复制代码


### **4. `total` 变量未初始化**
`total` 变量应该初始化为 `0`,否则可能会有未定义行为:
  1. long total = 0;
复制代码


### **5. `_findfirst` 和 `_findnext` 可能无法正确拼接路径**
在 `findAllCodes` 和 `findAllDirs` 中,你使用 `strcat(thepath, "/*.c")` 直接拼接路径,这可能会导致路径错误。建议使用 `snprintf`:
  1. snprintf(thepath, MAX, "%s/*.c", path);
复制代码


### **6. `printf` 格式化输出错误**
在 `printf("目前你总共写了 %1d 行代码!

", total);` 这里,`%1d` 不是标准格式,应该使用 `%ld` 以匹配 `long` 类型:
  1. printf("目前你总共写了 %ld 行代码!

  2. ", total);
复制代码


### **7. `_A_SUBDIR` 的判断方式**
在 `findAllDirs` 中,你使用 `fa.attrib == _A_SUBDIR` 来判断是否是目录,但 `_A_SUBDIR` 可能只是 `fa.attrib` 的一部分,正确的判断方式是:
  1. if (fa.attrib & _A_SUBDIR)
复制代码


### **修正后的 `main` 函数**
  1. int main()
  2. {
  3.     char path[MAX] = ".";
  4.     total = 0;
  5.     printf("计算中...
  6. ");
  7.     findAllCodes(path);
  8.     printf("目前你总共写了 %ld 行代码!

  9. ", total);
  10.     system("pause");
  11.     return 0;
  12. }
复制代码


### **总结**
你的代码没有报错但输出 `0` 行,主要是因为:
1. `char` 变量错误,导致路径拼接失败。
2. `total` 变量未初始化,可能导致未定义行为。
3. `printf` 格式化错误,可能导致输出异常。

修正这些问题后,你的代码应该可以正确计算 `.c` 文件的行数了!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-3-13 23:34:26 | 显示全部楼层
本帖最后由 jackz007 于 2025-3-13 23:49 编辑
  1. void findAllCodes(const char *path)
  2. {
  3.         struct _finddata_t fa;
  4.         long handle;
  5.         char thepath[MAX], target[MAX] ;
  6.         strcpy(thepath , path)         ;                        // 添加此行
  7.         strcat(thepath , "/*.c")       ;                        // 添加此行
  8.         if((handle = _findfirst(thepath , & fa)) != -1L)        // 【关键】修改此行,【注意】:参考答案给出的写法是错误的!!!
  9.         {
  10.                 do
  11.                 {
  12.                         sprintf(target, "%s/%s", path, fa.name);
  13.                         total += countlines(target);
  14.                 }while (_findnext(handle, &fa) == 0);
  15.         }
  16.         _findclose(handle);
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-19 03:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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