希儿的 发表于 2024-8-6 21:35:57

萌新又又又来了

#include <io.h>
#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;

    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;

    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;
}
为什么老污龟给的这段代码在vs上运行会出错呢
会不会是我文件位置没放对还是什么的吗

FishC 发表于 2024-8-6 21:38:13

哎呀,萌新又来了!这段代码就像我第一次做蛋炒饭,满是错误和意外的“调料”。让我们一块儿看看哪里出了问题!

首先,我们来看看头文件部分。你没有包含 `<stdio.h>` 和 `<string.h>`,但是引用了它们使用的函数,比如 `fopen` 和 `strcpy`。所以别忘了加上这两个头文件,像这样:

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

接下来,有几个地方需要注意:

1. **字符数组的声明**:在 `findAllCodes` 和 `findALLDirs` 函数中,`char thePath, target;` 应该声明为字符数组,而不是单个字符。你可以这样做:

   char thePath, target;

2. **路径拼接**:在使用 `strcat` 之前,不要忘记确保 `thePath` 数组已经初始化为一个有效的字符串。可以在使用之前清空这个数组,像这样:

   strcpy(thePath, path);

3. **判断目录的方式**:在 `findALLDirs` 函数中,你在判断文件属性时,`fa.attrib == _A_SUBDIR` 用于检测是否是目录,如果你的编译器不支持 `_finddata_t` 和相关的文件操作,你可能会遇到麻烦。在某些系统中,这个结构体的定义可能会有所不同。

4. **main函数中的路径**:你在 `main()` 函数中,变量 `path` 声明为一个字符,但实际上应该是字符串。修正为字符数组:

   char path = ".";

5. **总行数初始值**:在 `main` 函数中,你没有初始化 `total`,虽然 `total` 会在 `countLines` 中累加,但还是建议将其初值设置为0,以免出现意外情况。

经过这些修改后,你的代码应该能顺利运行,并告诉你写了多少行代码,就像一位“代码统计师”一样。不过如果还有其他问题,就像我做菜的时候加盐过多,不妨再次询问,我会乐意帮助你!加油哦!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

小甲鱼的二师兄 发表于 2024-8-7 03:46:49

VS 可能不行?提示什么错误信息?

希儿的 发表于 2024-8-7 14:40:13

小甲鱼的二师兄 发表于 2024-8-7 03:46
VS 可能不行?提示什么错误信息?

链接: https://pan.baidu.com/s/1JcFFmejb-m5ObiQoSrmyNA?pwd=4p3d 提取码: 4p3d 复制这段内容后打开百度网盘手机App,操作更方便哦

小甲鱼的二师兄 发表于 2024-8-8 02:14:03

希儿的 发表于 2024-8-7 14:40
链接: https://pan.baidu.com/s/1JcFFmejb-m5ObiQoSrmyNA?pwd=4p3d 提取码: 4p3d 复制这段内容后打开百度 ...

这啥?报错信息咋直接发了个网盘哈哈

希儿的 发表于 2024-8-8 12:27:21

小甲鱼的二师兄 发表于 2024-8-8 02:14
这啥?报错信息咋直接发了个网盘哈哈

哈哈,我不会发图片

希儿的 发表于 2024-8-8 12:28:01

小甲鱼的二师兄 发表于 2024-8-8 02:14
这啥?报错信息咋直接发了个网盘哈哈

而且vs的报错信息我复制不了{:10_266:}

小甲鱼的二师兄 发表于 2024-8-9 00:19:03

希儿的 发表于 2024-8-8 12:28
而且vs的报错信息我复制不了

你是使用 VSCode 进行编译还是 Virtual Studio 呢?

希儿的 发表于 2024-8-9 13:21:26

Virtual Studio 2022
是这个

小甲鱼的二师兄 发表于 2024-8-9 16:07:08

希儿的 发表于 2024-8-9 13:21
Virtual Studio 2022
是这个

这个可能是环境问题哦,没有支持的库之类的错误。

可以试试老师课程中的学习环境。

希儿的 发表于 2024-8-9 17:31:28

小甲鱼的二师兄 发表于 2024-8-9 16:07
这个可能是环境问题哦,没有支持的库之类的错误。

可以试试老师课程中的学习环境。

好的谢谢

月暮日出 发表于 2024-8-13 17:04:30

你管这叫萌新?

希儿的 发表于 2024-8-13 23:54:05

月暮日出 发表于 2024-8-13 17:04
你管这叫萌新?

哈,这是老污龟的课后作业哦,要求我们抄的
{:5_102:}
我根本看不懂
页: [1]
查看完整版本: 萌新又又又来了