gpt4 book ai didi

c - 我的 C 代码中的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:05 26 4
gpt4 key购买 nike

<分区>

我的代码中出现段错误,这是什么原因。 我认为这是因为 cur = readdir(dr); 中的 while 循环当我试图输入错误时,它没有进入 while 循环。

int main(char *source)
{
DIR *dr;
struct dirent *cur;
struct stat fi;
long int total_size = 0;
dr = opendir(source);
char *name=source;
size_t sourcelen = strlen(source);
printf("\n\n Source is %s\n\n\n", source);
printf("before *path \n");
char *path = malloc(100);
strcpy(path,source);
printf("before while \n");
while ((cur = readdir(dr)) != NULL)
{

if(cur->d_name[0] != '.')
{
free(path);
path = malloc(sourcelen + strlen(cur->d_name) + 2);
strcat(path,source);
strcat(path,"/");
strcat(path,cur->d_name);
if(stat(path, &fi) == -1)
{
printf("error \n\n");
}
else
{

printf("%s ",cur->d_name);
printf("%ld ",fi.st_blocks);
total_size = total_size + fi.st_blocks;
}
}
}
printf("\n\ntotal_size = %ld \n", total_size);
printf("\n\n\n");

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com