免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4062 | 回复: 14
打印 上一主题 下一主题

[C] [已解决]自己实现的cp -r ,可有个文件就是拷不到?请帮忙看看。。。多谢 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-28 23:22 |只看该作者 |倒序浏览
自己写的cp -r 实现copy目录下的所有文件目录,可怎么有个文件就是拷不到?
递归拷贝完一个目录后,就结束了,还有个文件没拷。。。。。
详细如下:
ls -al temp
总计 20
drwxr-xr-x  3 wwq wwq 4096 2009-03-29 07:07 .
drwxr-xr-x 27 wwq wwq 4096 2009-03-29 09:14 ..
-rw-r--r--  1 wwq wwq   13 2009-03-29 07:07 china
drwxr-xr-x  3 wwq wwq 4096 2009-03-23 07:27 mm
-rw-r--r--  1 wwq wwq   12 2009-03-29 07:07 wen

执行./a.out temp yy

temp/china is a file.
temp/mm is directory
temp/mm/xx is a file.
temp/mm/dd is directory
temp/mm/dd/nn is a file.
copyd_d  to  is ok
copyd_d temp/mm/dd to yy/mm/dd is ok
copyd_d temp/mm to yy/mm is ok

没有拷文件wen就退出了。。。。。

[ 本帖最后由 wenqing_9115 于 2009-4-30 09:41 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-03-28 23:22 |只看该作者

这是我的代码

#include <stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<limits.h>


#ifdef PATH_MAX
static int pathmax=PATH_MAX;
#endif

int copyf(FILE *,FILE *);
int copyf_d(char *,char *,char *);
int copyd_d(char *,char *);


int main(int argc,char *argv[])
{
  int i;
  char *d1;
  char *d2;
  d1=argv[1];
  d2=argv[2];
  i=copyd_d(d1,d2);
  if(i==0) printf("copy %s to %s success\n",argv[1],argv[2]);
  return 0;
}

int copyd_d(char *cpd1,char *cpd2){
   static DIR *dp1;
   static DIR *dp2;
   char *d_path1,*d_path2;
   char *path;
   path=malloc(pathmax+1);
   d_path1=malloc(pathmax+1);
   d_path2=malloc(pathmax+1);

   char b[200];
   static struct dirent *dir;
   static struct stat buf;

   if((dp1=opendir(cpd1))==NULL)
   {
    printf("\nCannot open the directory %s strike any key exit!",cpd1);
    getchar();
    exit(1);
   }
   if((dp2=opendir(cpd2))==NULL)
   {   
    if(mkdir(cpd2,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)<0)
       printf("\nCannot make the directory %s strike any key exit!",cpd2);
   }
   
   while((dir=readdir(dp1))!=NULL)
   {
   if(strcmp(dir->d_name,".")==0||strcmp(dir->d_name,"..")==0)
      continue;
   strcpy(path,cpd1);
   strcat(path,"/");
   strcat(path,dir->d_name);

   if(stat(path,&buf)<0)
   { printf("%s stat error\n",path);
      exit(1);
   }
   if(S_ISREG(buf.st_mode))
   {
     printf("%s is a file.\n",path);
     copyf_d(path,dir->d_name,cpd2);
   }
   else if(S_ISDIR(buf.st_mode))
    {
     printf("%s is directory\n",path);
     strcpy(d_path1,path);
     strcpy(d_path2,cpd2);
     strcat(d_path2,"/");
     strcat(d_path2,dir->d_name);
     copyd_d(d_path1,d_path2);
    }
   }
   printf("copyd_d %s to %s is ok\n",d_path1,d_path2);
    return 0;
}


int copyf_d(char *pathfile,char *filename,char *dir)   
{
   FILE *fp1;
   FILE *fp2;
   
   char buffer[MAXPATH];
   char *cwd;
   char *rdir;
   rdir=malloc(pathmax+1);
  if((fp1=fopen(pathfile,"r"))==NULL)
    {
    printf("\nCannot open the file1 %s strike any key exit!",pathfile);
    getchar();
    exit(1);
    }

   strcpy(rdir,dir);
   strcat(rdir,"/");
   strcat(rdir,filename);
   
   
   if((fp2=fopen(rdir,"w+"))==NULL)
    {
    printf("\nCannot open the file2 %s strike any key exit!",rdir);
    getchar();
    exit(0);
    }
    copyf(fp1,fp2);
   return 0;
}

int copyf(FILE *f1,FILE *f2)
{   
    char ch;
    ch=fgetc(f1);
    while(ch!=EOF)
        {
              fputc(ch,f2);
        ch=fgetc(f1);
        }
    return 0;
}

论坛徽章:
1
天蝎座
日期:2013-08-25 10:27:22
3 [报告]
发表于 2009-03-29 00:00 |只看该作者
我这都拷贝了。看了代码,没什么问题。
你去yy下看了没wen?)

以上为原帖回复。有误,参见13楼。

[ 本帖最后由 yangsf5 于 2009-3-31 09:05 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-03-29 00:48 |只看该作者
看了就是没有wen。。。。

论坛徽章:
0
5 [报告]
发表于 2009-03-29 00:51 |只看该作者
写了一个类似的代码遍历temp目录,就可以读到wen这个文件。。。
看了好多遍,也不知道怎么回事。。。

论坛徽章:
0
6 [报告]
发表于 2009-03-29 09:07 |只看该作者
这个用二进制打开文件会不会好点

论坛徽章:
0
7 [报告]
发表于 2009-03-29 10:08 |只看该作者
奇怪,只能自己调试了,一步一步跟踪看看

论坛徽章:
0
8 [报告]
发表于 2009-03-29 21:38 |只看该作者
嗯,好吧,只能自己再调试看看。。。。
系统的原因???总是出莫名奇妙的问题。。。。

论坛徽章:
0
9 [报告]
发表于 2009-03-29 21:59 |只看该作者
明天我也换台机子试试。。。。多谢啦。。。。

论坛徽章:
1
天蝎座
日期:2013-08-25 10:27:22
10 [报告]
发表于 2009-03-29 23:07 |只看该作者
还是昨天那个问题。。
我这里貌似程序在递归读完一个目录后就结束了,readdir就没有再继续读后面的文件了。。。。
你那里都能考过去么??会不会你那里刚好“ 文件都是在目录之前被读到了”???

都读到了。
我测试的是
xx/1     (文件)
xx/2   (目录)
xx/2/a (文件)
xx/3(文件)
全拷到了。

另外你的printf没打印全信息,不要受这个视觉上影响。)




以上为原帖回复。有误,参见13楼。

[ 本帖最后由 yangsf5 于 2009-3-31 09:05 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP