【发布时间】:2015-02-17 02:06:07
【问题描述】:
为了复制目录,我实现了以下功能。
int copy_dir(char *source, char *destination, char *file_name)
{
DIR *dir_ptr = NULL;
struct dirent *direntp;
char temp_dest[strlen(destination)+1];
char temp_src[strlen(source)+1];
strcat(destination, "/");
strcat(source, "/");
strcpy(temp_dest, destination);
strcpy(temp_src, source);
...
if( (dir_ptr = opendir(source)) == NULL )
{
fprintf(stderr, "Cannot open %s for copying\n", source);
return ERR_OPEN_DIR;
}
else
{
while(direntp = readdir(dir_ptr))
{
// File must already be on the USB key
if(strcmp(direntp->d_name,file_name) == 0)
{
strcat(temp_dest, direntp->d_name);
printf("after strcat temp_dest=%s\n", temp_dest);
strcat(temp_src, direntp->d_name);
printf("after strcat temp_src=%s\n", temp_src);
printf("destination:%s\n",temp_dest);
copy_files(temp_src, temp_dest);
}
}
}
closedir(dir_ptr);
return 1;
}
我想了解为什么下面的实现
strcat(temp_dest, direntp->d_name);
printf("after strcat temp_dest=%s\n", temp_dest);
strcat(temp_src, direntp->d_name);
printf("after strcat temp_src=%s\n", temp_src);
printf("destination:%s\n",temp_dest);
copy_files(temp_src,temp_dest);
返回:
after strcat temp_dest=/tmp/usb/test10
after strcat temp_src=/media/sda1/test10
destination:10
为什么通过最后一个 printf 返回 10 而不是 /tmp/usb/test10 ?
【问题讨论】:
-
你的
temp_dest声明和初始化在哪里? -
如果您不介意,可以提供MCVE吗?
-
@Sourav Ghosh - 我不知道 MCVE 是什么意思?您可以发送完整的表格吗?
-
对不起,我编辑了我的第一篇文章并包含了整个功能
-
@bhuvanesh 那里有一个链接。对于 OP:在本次比赛中,这意味着您提供了一个可以编译和执行的最小程序(即,不仅是完整的功能,还包括主文件和头文件)。这让人们更容易自己尝试并找出错误。
标签: c variables parameters parameter-passing strcat