为什么我报的是写了0行,没有给我报错
#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, target;
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;
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);
printf("目前你总共写了 %1d 行代码! \n\n", total);
system("pause");
return 0;
}
你的代码有几个问题,导致它没有正确计算代码行数,以下是主要问题和修正建议:
### **1. `char thepath, target;` 变量定义错误**
在 `findAllCodes` 和 `findAllDirs` 函数中,你定义了 `char thepath, target;`,但 `thepath` 和 `target` 应该是字符串数组,而不是单个 `char` 变量。正确的定义方式如下:
char thepath, target;
### **2. `sprintf(target, "%s/%s", path, fa.name);` 可能导致缓冲区溢出**
`target` 需要足够的空间来存储路径,建议使用 `snprintf` 以避免缓冲区溢出:
snprintf(target, MAX, "%s/%s", path, fa.name);
### **3. `char path = ".";` 变量定义错误**
在 `main` 函数中,你定义了 `char path = ".";`,但 `path` 应该是字符串数组:
char path = ".";
### **4. `total` 变量未初始化**
`total` 变量应该初始化为 `0`,否则可能会有未定义行为:
long total = 0;
### **5. `_findfirst` 和 `_findnext` 可能无法正确拼接路径**
在 `findAllCodes` 和 `findAllDirs` 中,你使用 `strcat(thepath, "/*.c")` 直接拼接路径,这可能会导致路径错误。建议使用 `snprintf`:
snprintf(thepath, MAX, "%s/*.c", path);
### **6. `printf` 格式化输出错误**
在 `printf("目前你总共写了 %1d 行代码!
", total);` 这里,`%1d` 不是标准格式,应该使用 `%ld` 以匹配 `long` 类型:
printf("目前你总共写了 %ld 行代码!
", total);
### **7. `_A_SUBDIR` 的判断方式**
在 `findAllDirs` 中,你使用 `fa.attrib == _A_SUBDIR` 来判断是否是目录,但 `_A_SUBDIR` 可能只是 `fa.attrib` 的一部分,正确的判断方式是:
if (fa.attrib & _A_SUBDIR)
### **修正后的 `main` 函数**
int main()
{
char path = ".";
total = 0;
printf("计算中...
");
findAllCodes(path);
printf("目前你总共写了 %ld 行代码!
", total);
system("pause");
return 0;
}
### **总结**
你的代码没有报错但输出 `0` 行,主要是因为:
1. `char` 变量错误,导致路径拼接失败。
2. `total` 变量未初始化,可能导致未定义行为。
3. `printf` 格式化错误,可能导致输出异常。
修正这些问题后,你的代码应该可以正确计算 `.c` 文件的行数了!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 本帖最后由 jackz007 于 2025-3-13 23:49 编辑
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thepath, target ;
strcpy(thepath , path) ; // 添加此行
strcat(thepath , "/*.c") ; // 添加此行
if((handle = _findfirst(thepath , & fa)) != -1L) // 【关键】修改此行,【注意】:参考答案给出的写法是错误的!!!
{
do
{
sprintf(target, "%s/%s", path, fa.name);
total += countlines(target);
}while (_findnext(handle, &fa) == 0);
}
_findclose(handle);
}
页:
[1]