递归调用后fileB和entry->d_name的值发生变化(backup(fileB,entry->d_name, archive_file)),为什么?我该如何解决这个问题?提前致谢!
else if(S_ISDIR(h.kind))
{
printf("\nits a directory #######\n");
struct dirent* entry = NULL;
DIR* dir = opendir(h.name);
if (dir == NULL) {
printf("Directory couldn't be opened\n");
exit(1);
}
while ((entry = readdir(dir))) {
printf("\n %s ********** \n %s ******* \n\n" , entry->d_name,fileB );
if (strcmp(".", entry->d_name) != 0 && strcmp("..", entry->d_name) != 0) {
printf("before recursion entry= %s , fileB= %s \n",entry->d_name,fileB);
backup(fileB,entry->d_name, archive_file);
printf("after recursion entry= %s , fileB= %s \n",entry->d_name,fileB);
}
}
if (closedir(dir)) {
printf("close dir error");
exit(1);
}
}
最佳答案
首先在备份函数中,您应该更改 const char* xxx
的参数。因为您不打算修改这些。因此,您将在函数中创建一个新的 char[] 或 char* :
char tmp[]; //you might have to give tmp specific size, use strlen()
strcpy(tmp, path);
strcat(tmp, "/");
strcat(tmp, fileB);
不确定它是否能解决您的问题,但有可能。
关于C - 从递归返回后指针值发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41692690/