|
发表于 2024-9-13 12:42:46
|
显示全部楼层
别急,让我们一步步来解决这个问题。根据您提供的代码,有几个显而易见的问题需要修正:
1. **头文件缺失**:您的 `#include` 语句中没有包含实际的头文件。
2. **类型定义错误**:变量 `thePath` 和 `target` 的声明需要改为字符数组。
3. **函数重定义**:函数 `readystatechange` 被定义了两次。
4. **路径拼接错误**:在路径拼接时,应该确保字符数组有足够的长度。
下面是修正后的代码:
- #include <stdio.h>
- #include <string.h>
- #include <io.h>
- #define max 256
- long total;
- int countlines(const char *filename);
- void findallcodes(const char *path);
- void findalldirs(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];
- strcpy(thePath, path);
- 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[] = "."; // 修改为字符数组
- printf("计算中...\n");
- findallcodes(path);
- findalldirs(path);
- printf("目前你总共写了 %ld 行代码!\n\n", total);
- system("pause");
- return 0;
- }
复制代码
**主要修改点总结**:
1. **头文件的修正**:在一开始需要包含实际使用的头文件,如 `stdio.h`, `string.h`, 和 `io.h`。
2. **字符数组声明**:需要将 `thePath` 和 `target` 定义为字符数组,并且在拼接时使用适当的路径分隔符 `/`。
3. **修复函数名重复**:删除重复定义。
这应修复大多数的语法和逻辑问题,希望这有帮到您。如果还有其他问题,请随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|