鱼C论坛

 找回密码
 立即注册
查看: 2408|回复: 6

求助哪个检测已经写了多少行的代码的问题!

[复制链接]
发表于 2023-10-18 15:33:49 | 显示全部楼层
这个问题是由于在使用 _findfirst 和 _findnext 函数时,没有正确关闭文件句柄导致的。在 findAllCodes 函数中,当 _findfirst 函数返回-1时,没有调用 _findclose 关闭文件句柄。这样会导致在下一次调用 _findfirst 函数时,会出现异常。

要解决这个问题,只需要在 findAllCodes 函数中添加一个判断语句,当 _findfirst 函数返回-1时,直接返回即可。

以下是修改后的代码:

  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.     handle = _findfirst(strcat(thePath, "/*.c"), &fa);
  8.     if (handle == -1L)
  9.     {
  10.         fprintf(stderr, "The path %s is wrong!\n", path);
  11.         return;
  12.     }

  13.     do
  14.     {
  15.         sprintf(target, "%s/%s", path, fa.name);
  16.         total += countLines(target);
  17.     } while (_findnext(handle, &fa) == 0);

  18.     _findclose(handle);
  19. }
复制代码


这样修改后,程序应该能够正常运行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-11 03:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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