鱼C论坛

 找回密码
 立即注册
查看: 251|回复: 3

[已解决]为什么课后作业S1E2中的代码,在vs2022不能编译成功?

[复制链接]
发表于 2024-4-30 16:37:42 | 显示全部楼层 |阅读模式

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

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

x
这是作业S1E2里的代码:
#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;
}
最佳答案
2024-4-30 16:38:06
在vs2022不能编译成功的原因可能是由于您使用了不兼容的库和函数。

首先,您在代码中包含了多个头文件,但是没有使用这些头文件中的函数和变量。所以可以去掉无用的头文件包含。

其次,您定义了一些字符串数组,但是没有给它们分配足够的空间。应该将字符串数组的定义改成足够大的字符数组(例如`char thePath[MAX]`和`char target[MAX]`)。

然后,您使用了非标准的函数`_findfirst`、`_findnext`和`_findclose`来遍历文件目录和文件。在新版本的Visual Studio中,可以使用标准的C函数`opendir`、`readdir`和`closedir`来代替。这些函数可以通过包含头文件`<dirent.h>`来使用。下面是使用标准库函数的修改代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.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)
{
    DIR *dir;
    struct dirent *entry;

    if ((dir = opendir(path)) == NULL)
    {
        fprintf(stderr, "Can not open the directory:%s\n", path);
        return;
    }

    while ((entry = readdir(dir)) != NULL)
    {
        if (entry->d_type == DT_REG && strstr(entry->d_name, ".c") != NULL)
        {
            char target[MAX];
            sprintf(target, "%s/%s", path, entry->d_name);
            total += countLines(target);
        }
        else if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
        {
            char subPath[MAX];
            sprintf(subPath, "%s/%s", path, entry->d_name);
            findAllCodes(subPath);
        }
    }

    closedir(dir);
}

void findAllDirs(const char *path)
{
    DIR *dir;
    struct dirent *entry;

    if ((dir = opendir(path)) == NULL)
    {
        fprintf(stderr, "Can not open the directory:%s\n", path);
        return;
    }

    while ((entry = readdir(dir)) != NULL)
    {
        if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
        {
            char subPath[MAX];
            sprintf(subPath, "%s/%s", path, entry->d_name);
            findAllCodes(subPath);
            findAllDirs(subPath);
        }
    }

    closedir(dir);
}

int main()
{
    char path[MAX] = ".";

    printf("计算中...\n");

    findAllCodes(path);
    findAllDirs(path);

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

    return 0;
}

希望这个修改后的代码可以在vs2022中成功编译运行并得到正确的结果。记得包含`<dirent.h>`头文件。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-30 16:38:06 | 显示全部楼层    本楼为最佳答案   
在vs2022不能编译成功的原因可能是由于您使用了不兼容的库和函数。

首先,您在代码中包含了多个头文件,但是没有使用这些头文件中的函数和变量。所以可以去掉无用的头文件包含。

其次,您定义了一些字符串数组,但是没有给它们分配足够的空间。应该将字符串数组的定义改成足够大的字符数组(例如`char thePath[MAX]`和`char target[MAX]`)。

然后,您使用了非标准的函数`_findfirst`、`_findnext`和`_findclose`来遍历文件目录和文件。在新版本的Visual Studio中,可以使用标准的C函数`opendir`、`readdir`和`closedir`来代替。这些函数可以通过包含头文件`<dirent.h>`来使用。下面是使用标准库函数的修改代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.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)
{
    DIR *dir;
    struct dirent *entry;

    if ((dir = opendir(path)) == NULL)
    {
        fprintf(stderr, "Can not open the directory:%s\n", path);
        return;
    }

    while ((entry = readdir(dir)) != NULL)
    {
        if (entry->d_type == DT_REG && strstr(entry->d_name, ".c") != NULL)
        {
            char target[MAX];
            sprintf(target, "%s/%s", path, entry->d_name);
            total += countLines(target);
        }
        else if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
        {
            char subPath[MAX];
            sprintf(subPath, "%s/%s", path, entry->d_name);
            findAllCodes(subPath);
        }
    }

    closedir(dir);
}

void findAllDirs(const char *path)
{
    DIR *dir;
    struct dirent *entry;

    if ((dir = opendir(path)) == NULL)
    {
        fprintf(stderr, "Can not open the directory:%s\n", path);
        return;
    }

    while ((entry = readdir(dir)) != NULL)
    {
        if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
        {
            char subPath[MAX];
            sprintf(subPath, "%s/%s", path, entry->d_name);
            findAllCodes(subPath);
            findAllDirs(subPath);
        }
    }

    closedir(dir);
}

int main()
{
    char path[MAX] = ".";

    printf("计算中...\n");

    findAllCodes(path);
    findAllDirs(path);

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

    return 0;
}

希望这个修改后的代码可以在vs2022中成功编译运行并得到正确的结果。记得包含`<dirent.h>`头文件。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-30 16:40:56 | 显示全部楼层
FishC 发表于 2024-4-30 16:38
在vs2022不能编译成功的原因可能是由于您使用了不兼容的库和函数。

首先,您在代码中包含了多个头文件, ...

那如果要继续使用`_findfirst`、`_findnext`和`_findclose`这些函数,vs2022要进行哪些设置或者代码中应当如何修改?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-30 16:41:20 | 显示全部楼层
海阔天空97 发表于 2024-04-30 16:40
那如果要继续使用`_findfirst`、`_findnext`和`_findc ...


如果您想继续使用`_findfirst`、`_findnext`和`_findclose`函数,您需要做以下设置或修改代码:

1. 首先,在VS2022中,创建一个C++项目,而不是默认的C项目。这是因为`_findfirst`等函数在C++中更常用。

2. 然后,您需要在代码开头添加以下包含文件:
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

这些头文件包含了与`_findfirst`等函数相关的声明。

3. 接下来,您需要将字符串数组的定义修改为足够大的字符数组,以便存储路径和文件名。例如:
char thePath[MAX];
char target[MAX];

4. 最后,修改函数`findALLDirs`的名称为`findALLFiles`,以与函数的实际功能相符。修改后的代码如下:
#include <io.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];
    char 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 findALLFiles(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);
            findALLFiles(thePath);
        }
    }while (_findnext(handle, &fa) == 0);

    _findclose(handle);   
}

int main()
{
    char path[MAX] = ".";

    printf("计算中...\n");

    findAllCodes(path);
    findALLFiles(path);

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

    return 0;
}

这样,您就可以在VS2022中继续使用`_findfirst`、`_findnext`和`_findclose`函数编译运行代码了。记得将项目设置为C++项目,并添加适当的头文件包含。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 07:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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