求助哪个检测已经写了多少行的代码的问题!
if((handle= _findfirst(strcat(thePath,"/*.c"),&fa))!=-1L){
do
{
sprintf(target,"%s/%s",path,fa.name);
total+=countLines(target);
}while( _findnext(handle,&fa)==0);
}
运行到这一段的时候提示出现异常。
Segmentation fault;
但是对照了完整的,的确没有地方写错,麻烦大佬们帮忙看看哪里出问题了,下发有完整代码!
#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("目前您总共写了%d行代码!请继续加油!!!\n\n",total);
system("pause");
return 0;
} 这个问题是由于在使用 _findfirst 和 _findnext 函数时,没有正确关闭文件句柄导致的。在 findAllCodes 函数中,当 _findfirst 函数返回-1时,没有调用 _findclose 关闭文件句柄。这样会导致在下一次调用 _findfirst 函数时,会出现异常。
要解决这个问题,只需要在 findAllCodes 函数中添加一个判断语句,当 _findfirst 函数返回-1时,直接返回即可。
以下是修改后的代码:
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath, target;
strcpy(thePath, path);
handle = _findfirst(strcat(thePath, "/*.c"), &fa);
if (handle == -1L)
{
fprintf(stderr, "The path %s is wrong!\n", path);
return;
}
do
{
sprintf(target, "%s/%s", path, fa.name);
total += countLines(target);
} while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
这样修改后,程序应该能够正常运行了。 你的代码看起来是在尝试遍历一个目录及其子目录,查找所有的.c文件,并计算这些文件中的行数。然而,你的代码中存在一些可能导致问题的地方。
1. 在函数findAllCodes和findALLDirs中,你使用了strcat(thePath,\"/*.c\")和strcat(thePath,\"/*\")。这可能会导致thePath数组越界,因为strcat函数会在目标字符串的末尾添加源字符串,而不考虑目标字符串的大小。这可能是导致段错误的原因。
2. 在函数findAllCodes和findALLDirs中,你在do...while循环中使用了sprintf函数来修改thePath,这可能会导致thePath的内容在每次循环中被改变,从而导致错误的路径。
3. 在函数findAllCodes中,你在_findfirst失败时没有关闭文件查找句柄。这可能会导致资源泄露。
为了解决这些问题,你可以尝试以下修改:
· 使用snprintf代替strcat和sprintf,以避免可能的数组越界问题。
· 在每次循环开始时,重新设置thePath的值。
· 在_findfirst失败时,确保关闭文件查找句柄。
这是修改后的findAllCodes函数:
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath,target;
snprintf(thePath, MAX, \"%s\", path);
snprintf(target, MAX, \"%s/*.c\", path);
if((handle= _findfirst(target,&fa))!=-1L)
{
do
{
snprintf(target, MAX, \"%s/%s\", path, fa.name);
total+=countLines(target);
snprintf(thePath, MAX, \"%s\", path);
}while( _findnext(handle,&fa)==0);
}
_findclose(handle);
}
请注意,这只是一个可能的解决方案,实际的问题可能需要更深入的调试才能找到。如果问题仍然存在,我建议你使用调试器来查看程序的运行情况,以便找到具体的错误位置。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案 问题分析:
根据提供的代码,可能导致Segmentation fault异常的原因可能有以下几点:
1. 文件路径超过了缓冲区大小。
2. 文件打开失败。
3. 文件未成功关闭。
4. 文件遍历时发生错误。
建议的解决方案:
1. 检查路径是否超过了缓冲区大小。使用strncat函数以限制拼接后的字符串长度,避免缓冲区溢出。
2. 在countLines函数内部,添加对文件打开失败的处理,确保文件能够成功打开。
3. 在countLines函数内部,添加对文件关闭失败的处理,确保文件能够成功关闭。
4. 检查文件遍历过程是否存在错误,例如文件不可读取等情况。
以下是修改后的代码示例:
#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 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++;
}
}
if (fclose(fp) != 0)
{
fprintf(stderr, "Can not close the file: %s\n", filename);
return 0;
}
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("目前您总共写了%d行代码!请继续加油!!!\n\n", total);
system("pause");
return 0;
}
希望这些修改能够解决您的问题。如果问题仍然存在,请提供完整的错误信息以便进一步排查。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:} isdkz 发表于 2023-10-18 15:33
这个问题是由于在使用 _findfirst 和 _findnext 函数时,没有正确关闭文件句柄导致的。在 findAllCodes 函 ...
你好,感谢解答,按照您的方法好像还是不行,提示和之前一样,但是我写的哪个代码在DEV C++里面运行又可以,在vstudio code里面就会报错,能麻烦再问下是什么原因吗,工作区显示代码本身没有写错。 Mike_python小 发表于 2023-10-18 15:35
问题分析:
根据提供的代码,可能导致Segmentation fault异常的原因可能有以下几点:
你好,感谢解答,按照您的方法好像还是不行,提示和之前一样,但是我写的哪个代码在DEV C++里面运行又可以,在vstudio code里面就会报错,能麻烦再问下是什么原因吗,工作区显示代码本身没有写错。 陶远航 发表于 2023-10-18 15:34
你的代码看起来是在尝试遍历一个目录及其子目录,查找所有的.c文件,并计算这些文件中的行数。然而,你的代 ...
你好,感谢解答,按照您的方法好像还是不行,提示和之前一样,但是我写的哪个代码在DEV C++里面运行又可以,在vstudio code里面就会报错,能麻烦再问下是什么原因吗,工作区显示代码本身没有写错。
页:
[1]