鱼C论坛

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

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

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

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

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

x
这是作业S1E2里的代码:

  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;

  8. int countLines(const char *filename);
  9. void findAllCodes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count = 0;
  15.         int temp;
  16.         
  17.         if ((fp = fopen(filename, "r")) == NULL)
  18.         {
  19.                 fprintf(stderr, "Can not open the file:%s\n", filename);
  20.                 return 0;
  21.         }
  22.         
  23.         while ((temp = fgetc(fp)) != EOF)
  24.         {
  25.                 if (temp == '\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.         }
  30.         
  31.         fclose(fp);
  32.         
  33.         return count;
  34. }

  35. void findAllCodes(const char *path)
  36. {
  37.         struct _finddata_t fa;
  38.         long handle;
  39.         char thePath[MAX], target[MAX];
  40.         
  41.         strcpy(thePath, path);
  42.         if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  43.         {
  44.                 do
  45.                 {
  46.                         sprintf(target, "%s/%s", path, fa.name);
  47.                         total += countLines(target);
  48.                 }while (_findnext(handle, &fa) == 0);
  49.         }
  50.    
  51.         _findclose(handle);
  52. }

  53. void findALLDirs(const char *path)
  54. {
  55.         struct _finddata_t fa;
  56.         long handle;
  57.         char thePath[MAX];
  58.         
  59.         strcpy(thePath, path);
  60.         if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  61.         {
  62.                 fprintf(stderr, "The path %s is wrong!\n",path);
  63.                 return;
  64.         }
  65.    
  66.         do
  67.         {        
  68.                 if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  69.                         continue;
  70.                     
  71.                 if( fa.attrib == _A_SUBDIR)
  72.                 {        
  73.                         sprintf(thePath, "%s/%s", path, fa.name);
  74.                         findAllCodes(thePath);
  75.                         findALLDirs(thePath);
  76.                 }
  77.         }while (_findnext(handle, &fa) == 0);
  78.    
  79.         _findclose(handle);   
  80. }

  81. int main()
  82. {
  83.         char path[MAX] = ".";
  84.         
  85.         printf("计算中...\n");
  86.         
  87.         findAllCodes(path);
  88.         findALLDirs(path);
  89.         
  90.         printf("目前你总共写了 %ld 行代码!\n\n", total);
  91.         system("pause");
  92.         
  93.         return 0;
  94. }
复制代码
最佳答案
2024-4-30 16:38:06
在vs2022不能编译成功的原因可能是由于您使用了不兼容的库和函数。

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

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

然后,您使用了非标准的函数`_findfirst`、`_findnext`和`_findclose`来遍历文件目录和文件。在新版本的Visual Studio中,可以使用标准的C函数`opendir`、`readdir`和`closedir`来代替。这些函数可以通过包含头文件`<dirent.h>`来使用。下面是使用标准库函数的修改代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>

  5. #define MAX 256

  6. long total;

  7. int countLines(const char *filename);
  8. void findAllCodes(const char *path);
  9. void findAllDirs(const char *path);

  10. int countLines(const char *filename)
  11. {
  12.     FILE *fp;
  13.     int count = 0;
  14.     int temp;

  15.     if ((fp = fopen(filename, "r")) == NULL)
  16.     {
  17.         fprintf(stderr, "Can not open the file:%s\n", filename);
  18.         return 0;
  19.     }

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

  27.     fclose(fp);

  28.     return count;
  29. }

  30. void findAllCodes(const char *path)
  31. {
  32.     DIR *dir;
  33.     struct dirent *entry;

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

  39.     while ((entry = readdir(dir)) != NULL)
  40.     {
  41.         if (entry->d_type == DT_REG && strstr(entry->d_name, ".c") != NULL)
  42.         {
  43.             char target[MAX];
  44.             sprintf(target, "%s/%s", path, entry->d_name);
  45.             total += countLines(target);
  46.         }
  47.         else if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
  48.         {
  49.             char subPath[MAX];
  50.             sprintf(subPath, "%s/%s", path, entry->d_name);
  51.             findAllCodes(subPath);
  52.         }
  53.     }

  54.     closedir(dir);
  55. }

  56. void findAllDirs(const char *path)
  57. {
  58.     DIR *dir;
  59.     struct dirent *entry;

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

  65.     while ((entry = readdir(dir)) != NULL)
  66.     {
  67.         if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
  68.         {
  69.             char subPath[MAX];
  70.             sprintf(subPath, "%s/%s", path, entry->d_name);
  71.             findAllCodes(subPath);
  72.             findAllDirs(subPath);
  73.         }
  74.     }

  75.     closedir(dir);
  76. }

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

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

  81.     findAllCodes(path);
  82.     findAllDirs(path);

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

  85.     return 0;
  86. }
复制代码


希望这个修改后的代码可以在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>`来使用。下面是使用标准库函数的修改代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>

  5. #define MAX 256

  6. long total;

  7. int countLines(const char *filename);
  8. void findAllCodes(const char *path);
  9. void findAllDirs(const char *path);

  10. int countLines(const char *filename)
  11. {
  12.     FILE *fp;
  13.     int count = 0;
  14.     int temp;

  15.     if ((fp = fopen(filename, "r")) == NULL)
  16.     {
  17.         fprintf(stderr, "Can not open the file:%s\n", filename);
  18.         return 0;
  19.     }

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

  27.     fclose(fp);

  28.     return count;
  29. }

  30. void findAllCodes(const char *path)
  31. {
  32.     DIR *dir;
  33.     struct dirent *entry;

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

  39.     while ((entry = readdir(dir)) != NULL)
  40.     {
  41.         if (entry->d_type == DT_REG && strstr(entry->d_name, ".c") != NULL)
  42.         {
  43.             char target[MAX];
  44.             sprintf(target, "%s/%s", path, entry->d_name);
  45.             total += countLines(target);
  46.         }
  47.         else if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
  48.         {
  49.             char subPath[MAX];
  50.             sprintf(subPath, "%s/%s", path, entry->d_name);
  51.             findAllCodes(subPath);
  52.         }
  53.     }

  54.     closedir(dir);
  55. }

  56. void findAllDirs(const char *path)
  57. {
  58.     DIR *dir;
  59.     struct dirent *entry;

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

  65.     while ((entry = readdir(dir)) != NULL)
  66.     {
  67.         if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
  68.         {
  69.             char subPath[MAX];
  70.             sprintf(subPath, "%s/%s", path, entry->d_name);
  71.             findAllCodes(subPath);
  72.             findAllDirs(subPath);
  73.         }
  74.     }

  75.     closedir(dir);
  76. }

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

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

  81.     findAllCodes(path);
  82.     findAllDirs(path);

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

  85.     return 0;
  86. }
复制代码


希望这个修改后的代码可以在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. 然后,您需要在代码开头添加以下包含文件:

  1. #include <io.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
复制代码


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

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

  1. char thePath[MAX];
  2. char target[MAX];
复制代码


4. 最后,修改函数`findALLDirs`的名称为`findALLFiles`,以与函数的实际功能相符。修改后的代码如下:

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

  5. #define MAX 256

  6. long total;

  7. int countLines(const char *filename);
  8. void findAllCodes(const char *path);
  9. void findALLFiles(const char *path);

  10. int countLines(const char *filename)
  11. {
  12.     FILE *fp;
  13.     int count = 0;
  14.     int temp;

  15.     if ((fp = fopen(filename, "r")) == NULL)
  16.     {
  17.         fprintf(stderr, "Can not open the file:%s\n", filename);
  18.         return 0;
  19.     }

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

  27.     fclose(fp);

  28.     return count;
  29. }

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

  36.     strcpy(thePath, path);
  37.     if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  38.     {
  39.         do
  40.         {
  41.             sprintf(target, "%s/%s", path, fa.name);
  42.             total += countLines(target);
  43.         }while (_findnext(handle, &fa) == 0);
  44.     }

  45.     _findclose(handle);
  46. }

  47. void findALLFiles(const char *path)
  48. {
  49.     struct _finddata_t fa;
  50.     long handle;
  51.     char thePath[MAX];

  52.     strcpy(thePath, path);
  53.     if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  54.     {
  55.         fprintf(stderr, "The path %s is wrong!\n",path);
  56.         return;
  57.     }

  58.     do
  59.     {        
  60.         if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  61.             continue;

  62.         if( fa.attrib == _A_SUBDIR)
  63.         {        
  64.             sprintf(thePath, "%s/%s", path, fa.name);
  65.             findAllCodes(thePath);
  66.             findALLFiles(thePath);
  67.         }
  68.     }while (_findnext(handle, &fa) == 0);

  69.     _findclose(handle);   
  70. }

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

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

  75.     findAllCodes(path);
  76.     findALLFiles(path);

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

  79.     return 0;
  80. }
复制代码


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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 21:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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