鱼C论坛

 找回密码
 立即注册
查看: 2306|回复: 2

[已解决]第一次课后作业求助,用的vscode,总是报错

[复制链接]
发表于 2023-2-27 11:39:56 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 moonbeams 于 2023-2-27 11:44 编辑

源代码如下:
  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.         if((fp = fopen(filename, "r")) == NULL)
  17.         {
  18.                 fprintf(stderr, "Can not open the file:%s\n",filename);
  19.                 return 0;
  20.         }

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

  28.         fclose(fp);

  29.         return count;
  30. }

  31. void findAllCodes(const char *path)
  32. {
  33.         struct _finddata_t fa;
  34.         long handle;
  35.         char thePath[MAX],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 findALLDirs(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.                         findALLDirs(thePath);
  67.                 }
  68.         }while(_findnext(handle, &fa) == 0);//会在这里报错:Segmentation fault;权限不够,发不了图片

  69.         _findclose(handle);
  70. }

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

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

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

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

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

权限不够发不了图片,报错内容在上面注释了
最佳答案
2023-5-14 19:28:21
根据代码中的注释,“Segmentation fault”是一个权限不够的错误。通常情况下,访问了系统保护内存区域导致的。但是,在这个实现中,这种错误可能会发生在进行目录遍历查找时。具体来说,当处理嵌套目录的过程中,可能会出现路径超长的情况,导致数组越界或者缓冲区溢出,从而访问到系统保护内存区域,使程序中止并报“Segmentation Fault”错误。

为了改正这一点,可以考虑使用更加安全的字符串拼接方式,例如 strncat 或者 snprintf 函数,来保证拼接后字符串长度不会超过指定的范围。此外,还可以在代码开头 #define MAX 更改为更大的值,以便能够容纳更长的路径名。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-6 14:42:42 | 显示全部楼层
你好 我的也是这里出了错,我也是VS,报的错误0xC0000005: 写入位置 0xFFFFFFFFB3FCE7F0 时发生访问冲突,如果你的也是请问你现在解决了吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 19:28:21 | 显示全部楼层    本楼为最佳答案   
根据代码中的注释,“Segmentation fault”是一个权限不够的错误。通常情况下,访问了系统保护内存区域导致的。但是,在这个实现中,这种错误可能会发生在进行目录遍历查找时。具体来说,当处理嵌套目录的过程中,可能会出现路径超长的情况,导致数组越界或者缓冲区溢出,从而访问到系统保护内存区域,使程序中止并报“Segmentation Fault”错误。

为了改正这一点,可以考虑使用更加安全的字符串拼接方式,例如 strncat 或者 snprintf 函数,来保证拼接后字符串长度不会超过指定的范围。此外,还可以在代码开头 #define MAX 更改为更大的值,以便能够容纳更长的路径名。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 16:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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