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

本文介绍了一个用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;
}


在C语言实现复制文件夹及其内容到目标目录的功能,可以通过递归遍历源目录中的文件目录,并将每个文件复制到目标目录中。由于C语言标准库本身不提供直接操作文件夹的功能,因此需要依赖操作系统提供的API,例如在Windows上使用`Windows.h`头文件中的函数,或者在Linux上使用`dirent.h``sys/stat.h`等头文件中的函数。 以下是一个适用于Windows平台的示例实现,使用了`FindFirstFile`、`FindNextFile`、`CreateDirectory`等Windows API来遍历复制目录内容: ```c #include <windows.h> #include <stdio.h> // 复制单个文件 int copyFile(const char *source, const char *dest) { if (CopyFile(source, dest, FALSE) == 0) { printf("Failed to copy file: %s\n", source); return 0; } return 1; } // 递归复制目录及其内容 void copyDirectory(const char *sourceDir, const char *targetDir) { WIN32_FIND_DATA findFileData; HANDLE hFind; char sourcePath[MAX_PATH]; char targetPath[MAX_PATH]; // 构造查找路径 sprintf(sourcePath, "%s\\*", sourceDir); hFind = FindFirstFile(sourcePath, &findFileData); if (hFind == INVALID_HANDLE_VALUE) { printf("Invalid directory handle.\n"); return; } // 创建目标目录 CreateDirectory(targetDir, NULL); do { // 跳过 . .. if (strcmp(findFileData.cFileName, ".") == 0 || strcmp(findFileData.cFileName, "..") == 0) { continue; } // 构造源路径目标路径 sprintf(sourcePath, "%s\\%s", sourceDir, findFileData.cFileName); sprintf(targetPath, "%s\\%s", targetDir, findFileData.cFileName); if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // 如果是目录,递归复制 copyDirectory(sourcePath, targetPath); } else { // 如果是文件,复制文件 copyFile(sourcePath, targetPath); } } while (FindNextFile(hFind, &findFileData) != 0); FindClose(hFind); } int main() { const char *source = "C:\\path\\to\\source"; const char *target = "C:\\path\\to\\target"; copyDirectory(source, target); printf("Directory copied successfully.\n"); return 0; } ``` 上述代码中,`copyFile`函数负责复制单个文件,`copyDirectory`函数则负责递归地复制整个目录及其子目录内容。需要注意的是,该代码仅适用于Windows平台。如果在Linux或其他类Unix系统上开发,则需要使用POSIX标准库函数,如`opendir`、`readdir`等来实现类似功能。 对于Linux平台,可以参考如下实现片段: ```c #include <stdio.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> #include <unistd.h> int copyFile(const char *source, const char *dest) { FILE *src = fopen(source, "rb"); FILE *dst = fopen(dest, "wb"); if (!src || !dst) { perror("File open failed"); return 0; } char buffer[4096]; size_t bytesRead; while ((bytesRead = fread(buffer, 1, sizeof(buffer), src)) > 0) { fwrite(buffer, 1, bytesRead, dst); } fclose(src); fclose(dst); return 1; } void copyDirectory(const char *sourceDir, const char *targetDir) { DIR *dir = opendir(sourceDir); if (!dir) { perror("Failed to open directory"); return; } struct dirent *entry; struct stat statbuf; char sourcePath[PATH_MAX], targetPath[PATH_MAX]; // 创建目标目录 mkdir(targetDir, 0777); while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } snprintf(sourcePath, sizeof(sourcePath), "%s/%s", sourceDir, entry->d_name); snprintf(targetPath, sizeof(targetPath), "%s/%s", targetDir, entry->d_name); if (lstat(sourcePath, &statbuf) == -1) { continue; } if (S_ISDIR(statbuf.st_mode)) { // 如果是目录,递归复制 copyDirectory(sourcePath, targetPath); } else { // 如果是文件,复制文件 copyFile(sourcePath, targetPath); } } closedir(dir); } int main() { const char *source = "/path/to/source"; const char *target = "/path/to/target"; copyDirectory(source, target); printf("Directory copied successfully.\n"); return 0; } ``` 在Linux平台上,`copyDirectory`函数使用`opendir`打开目录,`readdir`读取目录内容,`mkdir`创建目标目录,`copyFile`通过文件流复制文件内容。这种方式更通用,适用于大多数POSIX兼容系统。 ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值