|
发表于 2023-10-18 15:33:49
|
显示全部楼层
这个问题是由于在使用 _findfirst 和 _findnext 函数时,没有正确关闭文件句柄导致的。在 findAllCodes 函数中,当 _findfirst 函数返回-1时,没有调用 _findclose 关闭文件句柄。这样会导致在下一次调用 _findfirst 函数时,会出现异常。
要解决这个问题,只需要在 findAllCodes 函数中添加一个判断语句,当 _findfirst 函数返回-1时,直接返回即可。
以下是修改后的代码:
- void findAllCodes(const char *path)
- {
- struct _finddata_t fa;
- long handle;
- char thePath[MAX], target[MAX];
- strcpy(thePath, path);
- handle = _findfirst(strcat(thePath, "/*.c"), &fa);
- if (handle == -1L)
- {
- fprintf(stderr, "The path %s is wrong!\n", path);
- return;
- }
- do
- {
- sprintf(target, "%s/%s", path, fa.name);
- total += countLines(target);
- } while (_findnext(handle, &fa) == 0);
- _findclose(handle);
- }
复制代码
这样修改后,程序应该能够正常运行了。 |
|