鱼C论坛

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

机器人帮我检查一下代码哪里错了

[复制链接]
发表于 2024-12-5 23:02:11 | 显示全部楼层 |阅读模式

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

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

x
机器人帮我检查一下代码哪里错了,如下:


#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX  256

long 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* char)
{
        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[MAX] = ".";
        printf("conculate.....\n");
        findAllCodes(path);
        findALLDirs(path);

        printf("there are all %1d codes! \n\n", total);
        system("pause");

        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-12-5 23:02:24 | 显示全部楼层
以下是你代码中的一些问题和建议:

1. **头文件缺失**:你在代码中使用了`struct _finddata_t`和`_findfirst`等函数,这些函数需要包含`<io.h>`头文件。

2. **字符串格式化错误**:在`fprintf`函数中,格式化字符串的使用不正确。应该是`fprintf(stderr, "can not open the file: %s
", filename);`。

3. **变量声明错误**:在`findAllCodes`和`findALLDirs`函数中,`char thePath, target;`和`char thePath;`应该是字符数组而不是单个字符。可以声明为`char thePath[MAX], target[MAX];`。

4. **函数参数命名冲突**:在`findAllCodes`函数中,参数名`char`是C语言的关键字,应该更改为其他名称,比如`const char* path`。

5. **未定义变量**:`total`变量在代码中未定义,需要在全局或适当的作用域中定义并初始化。

6. **字符串操作错误**:在`strcpy`和`strcat`函数中,确保目标数组有足够的空间来存储结果。

7. **格式化字符串错误**:在`fprintf`函数中,`"The path %s is wrong!"
,path`应该是`"The path %s is wrong!
", path`。

8. **字符数组初始化错误**:在`main`函数中,`char path = ".";`应该是`char path[] = ".";`。

9. **格式化输出错误**:在`printf`中,`"%1d"`应该是`"%d"`。

根据以上问题,建议你逐一修正代码。希望这些建议能帮助你解决问题!如果有其他问题,请随时问我。

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

使用道具 举报

 楼主| 发表于 2024-12-5 23:05:34 | 显示全部楼层
FishC 发表于 2024-12-5 23:02
以下是你代码中的一些问题和建议:

1. **头文件缺失**:你在代码中使用了`struct _finddata_t`和`_findf ...

我写了
#include <io.h>
代码了,为什么还说我头文件缺失
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-12-5 23:12:27 | 显示全部楼层
0.3 发表于 2024-12-05 23:05
我写了
#include
代码了,为什么还说我头文件缺失


在你的代码中,`#include`语句后面没有指定具体的头文件名称。你需要确保每个`#include`语句后面都有一个有效的头文件名称,比如`#include <stdio.h>`。以下是你可能需要的头文件:

- `#include <stdio.h>`:用于`printf`、`fprintf`、`fopen`、`fclose`等标准输入输出函数。
- `#include <string.h>`:用于`strcpy`、`strcat`、`strcmp`等字符串操作函数。
- `#include <io.h>`:用于`_findfirst`、`_findnext`、`_findclose`等文件查找函数。
- `#include <stdlib.h>`:用于`system`函数。

确保在代码中正确包含这些头文件。

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

使用道具 举报

发表于 2024-12-6 00:36:57 | 显示全部楼层
total 变量未定义
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-12-6 00:38:21 | 显示全部楼层
const char* char 这个也不对,char 是 C 的关键字,不能作为变量名。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-12-6 00:39:26 | 显示全部楼层
试试这个:

  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX 256

  7. long total = 0;

  8. long countlines(const char* filename)
  9. {
  10.     FILE* fp;
  11.     int count = 0;
  12.     int temp;
  13.     if ((fp = fopen(filename, "r")) == NULL)
  14.     {
  15.         fprintf(stderr, "Cannot open the file: %s\n", filename);
  16.         return -1;
  17.     }

  18.     while ((temp = fgetc(fp)) != EOF)
  19.     {
  20.         if (temp == '\n')
  21.         {
  22.             count++;
  23.         }
  24.     }
  25.     fclose(fp);
  26.     return count;
  27. }

  28. void findAllCodes(const char* path)
  29. {
  30.     struct _finddata_t fa;
  31.     long handle;
  32.     char thePath[MAX], target[MAX];

  33.     snprintf(thePath, MAX, "%s/*.c", path);
  34.     if ((handle = _findfirst(thePath, &fa)) != -1L)
  35.     {
  36.         do
  37.         {
  38.             snprintf(target, MAX, "%s/%s", path, fa.name);
  39.             long lines = countlines(target);
  40.             if (lines >= 0)
  41.                 total += lines;

  42.         } while (_findnext(handle, &fa) == 0);
  43.     }

  44.     _findclose(handle);
  45. }

  46. void findALLDirs(const char* path)
  47. {
  48.     struct _finddata_t fa;
  49.     long handle;
  50.     char thePath[MAX], newPath[MAX];

  51.     snprintf(thePath, MAX, "%s/*", path);
  52.     if ((handle = _findfirst(thePath, &fa)) == -1L)
  53.     {
  54.         fprintf(stderr, "The path %s is wrong!\n", path);
  55.         return;
  56.     }
  57.     do
  58.     {
  59.         if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  60.             continue;
  61.         if (fa.attrib & _A_SUBDIR)
  62.         {
  63.             snprintf(newPath, MAX, "%s/%s", path, fa.name);
  64.             findAllCodes(newPath);
  65.             findALLDirs(newPath);
  66.         }
  67.     } while (_findnext(handle, &fa) == 0);

  68.     _findclose(handle);
  69. }

  70. int main()
  71. {
  72.     char path[MAX] = ".";
  73.     printf("Calculating...\n");
  74.     findAllCodes(path);
  75.     findALLDirs(path);

  76.     printf("There are %ld lines of code in total!\n\n", total);
  77.     system("pause");

  78.     return 0;
  79. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 01:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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