鱼C论坛

 找回密码
 立即注册
查看: 1161|回复: 9

c语言求助

[复制链接]
发表于 2023-5-14 22:35:58 | 显示全部楼层 |阅读模式

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

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

x
第一次课后作业求助  用的Visual Studio 2022  运行的时候显示 发生生成错误
代码如下:
#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[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("计算中...\n");

        findALLCodes(path);
        findALLDirs(path);

        printf("目前你总共写了 %ld 行代码!\n\n", total);
        system("pause");

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-14 22:36:12 | 显示全部楼层
问题出在函数名字拼写错误,正确的函数名应该是  findAllCodes ,而不是  findALLCodes ,因此在调用  findALLDirs  函数时会出现错误。将  findALLCodes  改成  findAllCodes  即可解决问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-14 22:49:08 | 显示全部楼层
isdkz 发表于 2023-5-14 22:36
问题出在函数名字拼写错误,正确的函数名应该是  findAllCodes ,而不是  findALLCodes ,因此在调用  find ...

改了之后还是不行,然后我试试把答案复制进去也是不行,这是为什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 22:58:36 | 显示全部楼层
在检查你的代码后,发现几处问题可能会导致编译错误:

  • findAllCodes() 函数名被拼写错误,应该为 findALLCodes()(大小写也要匹配)。
  • 在 findALLDirs() 函数中,变量 thePath 与 _findfirst() 中包含的路径拼接起来可能会超过 MAX 缓冲区大小。可以使用 strncat() 函数代替 strcat() 避免这个问题。
  • 需要在文件开头增加 #define _CRT_SECURE_NO_WARNINGS 来避免 fopen() 等函数被标记为不安全的警告。

修改后的代码如下:

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

#define MAX 256
#define _CRT_SECURE_NO_WARNINGS

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(strncat(thePath, "/*", MAX), &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("计算中...\n");

    findALLCodes(path);
    findALLDirs(path);

    printf("目前你总共写了 %ld 行代码!\n\n", total);
    system("pause");

    return 0;
}
希望这些调整可以帮助你成功编译和运行代码。

实在不行用Dev_C++
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-5-14 22:58:37 | 显示全部楼层
你好,这段代码存在两个小问题导致编译失败:

首先,函数名大小写不一致。你定义了一个 findAllCodes 函数,但在程序中调用时却使用了 findALLCodes ,导致编译器无法找到正确的函数进行链接。可以将 findAllCodes 修改为 findALLCodes 以匹配调用。

其次,在主函数中没有包含文件头 io.h 和 _finddata_t 结构体的定义。你可以添加 # include <direct.h> 来解决此问题。

修改后的完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <direct.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)                
} 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("计算中...\n");

        findALLCodes(path);
        findALLDirs(path);

        printf("目前你总共写了 %ld 行代码!\n\n", total);
        system("pause");

        return 0;
}
修改后的缩进可能不对,自己调一下哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 23:00:26 | 显示全部楼层
歌者文明清理员 发表于 2023-5-14 22:58
在检查你的代码后,发现几处问题可能会导致编译错误:

58s — 歌者文明清理员
57s — sfqxx
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 23:05:52 | 显示全部楼层
wuhu~起飞 发表于 2023-5-14 22:49
改了之后还是不行,然后我试试把答案复制进去也是不行,这是为什么

具体的报错信息有吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 23:06:14 | 显示全部楼层
sfqxx 发表于 2023-5-14 23:00
58s — 歌者文明清理员
57s — sfqxx

有啥好惊讶的,我和isdkz有一回差了1秒不到,他领先
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 23:07:44 | 显示全部楼层
歌者文明清理员 发表于 2023-5-14 23:06
有啥好惊讶的,我和isdkz有一回差了1秒不到,他领先

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 23:32:34 | 显示全部楼层
wuhu~起飞 发表于 2023-5-14 22:49
改了之后还是不行,然后我试试把答案复制进去也是不行,这是为什么

有详细·报错信息吗,或者是你编译器问题??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 15:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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