Linux下c语言实现将一个目录下的所有文件和目录复制到另一个目录下

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

char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];

void Copy(char *spathname,char *tpathname)
{
   int sfd,tfd;
   struct stat s,t;
   char c;
   sfd=open(spathname,O_RDONLY);
   tfd=open(tpathname,O_RDWR|O_CREAT);
   while(read(sfd,&c,1)>0)
        write(tfd,&c,1);
   fstat(sfd,&s);
   chown(tpathname,s.st_uid,s.st_gid);
   chmod(tpathname,s.st_mode);
  
   close(sfd);
   close(tfd);
}

void d_copy(char *spathname,char *tpathname)
{
   struct stat s,t,temp_s,temp_t;
   struct dirent *s_p;
   DIR *dirs,*dirt;

    

     stat(spathname,&s);
   mkdir(tpathname,s.st_mode);
   chown(tpathname,s.st_uid,s.st_gid);
   dirt=opendir(tpathname);
   dirs=opendir(spathname);
   strcpy(temp_paths,spathname);
   strcpy(temp_patht,tpathname);
   while((s_p=readdir(dirs))!=NULL)
   {
      if(strcmp(s_p->d_name,".")!=0&&strcmp(s_p->d_name,"..")!=0)
      {
          strcat(temp_paths,"/");
          strcat(temp_paths,s_p->d_name);
          strcat(temp_patht,"/");
          strcat(temp_patht,s_p->d_name);
          lstat(temp_paths,&s);
          temp_s.st_mode=s.st_mode;
          if(S_ISLNK(temp_s.st_mode))
          {
              printf("%s is a symbol link file\n",temp_paths);
          }
          else if(S_ISREG(temp_s.st_mode))
          {
              printf("Copy file %s ......\n",temp_paths);
              Copy(temp_paths,temp_patht);
              strcpy(temp_paths,spathname);

                strcpy(temp_patht,tpathname);

            }
          else if(S_ISDIR(temp_s.st_mode))
          {
              printf("Copy directory %s ......\n",temp_paths);
              d_copy(temp_paths,temp_patht);
              strcpy(temp_paths,spathname);
              strcpy(temp_patht,tpathname);
          }
      }
   }
}

int main()
{
   struct dirent *sp,*tp;
   char spath[1000],tpath[1000],temp_spath[1000],temp_tpath[1000];
   struct stat sbuf,tbuf,temp_sbuf,temp_tbuf;
   char sdirect[1000],tdirect[1000],judge;
   DIR *dir_s,*dir_t;
  
   printf("Please input the sourse direct path and name :");
   scanf("%s",sdirect);
   dir_s=opendir(sdirect);
   if(dir_s==NULL)
   {
      printf("This directory don't exist !\n");
      return 0;
   }
   if(stat(sdirect,&sbuf)!=0)
   {
      printf("Get status error !\n");
      return 0;
   }
   printf("Please input the target direct path and name :");
   scanf("%s",tdirect);
   dir_t=opendir(tdirect);
   if(dir_t==NULL)
   {
      mkdir(tdirect,sbuf.st_mode);
      chown(tdirect,sbuf.st_uid,sbuf.st_gid);
      dir_t=opendir(tdirect);
   }
   else
   {
      chmod(tdirect,sbuf.st_mode);
      chown(tdirect,sbuf.st_uid,sbuf.st_gid);
   }  
   strcpy(spath,sdirect);
   strcpy(tpath,tdirect);
   strcpy(temp_spath,sdirect);
   strcpy(temp_tpath,tdirect);
/
   printf("Begin copy ........\n");
   while((sp=readdir(dir_s))!=NULL)
   {
      if(strcmp(sp->d_name,".")!=0&&strcmp(sp->d_name,"..")!=0)
      {
          strcat(temp_spath,"/");
          strcat(temp_spath,sp->d_name);
          strcat(temp_tpath,"/");
          strcat(temp_tpath,sp->d_name);
          lstat(temp_spath,&sbuf);
          temp_sbuf.st_mode=sbuf.st_mode;
          if(S_ISLNK(temp_sbuf.st_mode))
          {
              printf("%s is a symbolic link file\n",temp_spath);
          }
          else if((S_IFMT&temp_sbuf.st_mode)==S_IFREG)
          {
              printf("Copy file %s ......\n",temp_spath);
              Copy(temp_spath,temp_tpath);
              strcpy(temp_tpath,tpath);
              strcpy(temp_spath,spath);
          }
          else if((S_IFMT&temp_sbuf.st_mode)==S_IFDIR)
          {
              printf("Copy directory %s ......\n",temp_spath);
              d_copy(temp_spath,temp_tpath);
              strcpy(temp_tpath,tpath);
              strcpy(temp_spath,spath);
          }
      }
   }

   printf("Copy end !\n");
   closedir(dir_t);
   closedir(dir_s);
   return 1;
}


转载自:http://biancheng.dnbcw.info/linux/74567.html 

Linux系统下,可以使用C语言编写一个文件复制程序来实现将file1.txt文件复制到指定目录。 首先,我们需要包含一些必要的头文件,如stdio.h和stdlib.h。接下来,我们定义一些变量来存储文件名,目标目录和相关的文件指针。 然后,我们打开原始文件file1.txt并进行错误检查。如果文件存在且可读,则创建一个新的目标文件,并打开它进行写操作。同样,我们需要进行错误检查。 接下来,在循环中,我们一次读取file1.txt中的数据,并将其写入目标文件中,直到文件结束。读取和写入可以使用fread()和fwrite()函数来完成。 最后,我们关闭原始文件和目标文件,并释放相关的资源。 以下是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> int main() { FILE *source_file, *target_file; char source_path[] = "file1.txt"; char target_path[] = "/path/to/destination/directory/file1.txt"; char buffer[1024]; size_t bytes; // 打开原始文件 source_file = fopen(source_path, "rb"); if (source_file == NULL) { printf("无法打开原始文件\n"); return 1; } // 创建目标目录并打开目标文件 target_file = fopen(target_path, "wb"); if (target_file == NULL) { printf("无法创建目标文件\n"); return 1; } while ((bytes = fread(buffer, 1, sizeof(buffer), source_file)) > 0) { fwrite(buffer, 1, bytes, target_file); } // 关闭文件 fclose(source_file); fclose(target_file); return 0; } ``` 以上就是一个简单的Linux系统下使用C语言实现文件复制程序的例子。你可以将file1.txt替换为你想要复制文件的名称,将target_path替换为你想要复制到的目录的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值