![](/img/trans.png)
[英]How to find a value in array and copy it to another one in C language?
[英]How to copy the directory from one place to another using C language?
我在網上搜索過,但沒有解決方案。 我想使用 C 語言將目錄/文件夾從一個地方復制到另一個地方。 雖然可以在命令行中使用cp
但我想用 C 語言來實現它。
要復制文件夾,包括其中的所有文件和子文件夾,您需要遍歷文件夾的內容,並為每個“條目”檢查它是文件還是文件夾
要遍歷文件夾的內容,您可以使用dirent.h
中的readdir
,檢查條目是否是文件夾或文件lstat
和sys/stat.h
S_ISDIR
sys/stat.h
夾mkdir
。
這需要一些修飾(它應該處理目標目錄的完整路徑,應該考慮邊緣情況等),但這是一個合理的開始:
/* Recursively duplicate a directory tree */
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static DIR * xopendir(const char *);
static void xclosedir(DIR *, const char *);
static void xstat(const char *, struct stat *);
static void xrealpath(const char *s, char *resolved);
static void pathcat(char *p, char *n);
struct dir { DIR *d; char *path, *end; };
static void
build_dir(char *spath, char *dpath, char *name, mode_t mod)
{
struct dirent *f;
struct dir src;
struct dir dst;
src.end = strchr(spath, '\0');
dst.end = strchr(dpath, '\0');
pathcat(dpath, name);
if( mkdir(dpath, mod) == -1 && errno != EEXIST ){
err(EXIT_FAILURE, "%s", name);
}
if( getenv("V") ){
printf("%s -> %s\n", spath, dpath);
}
src.d = xopendir(src.path = spath);
while( (f = readdir(src.d)) != NULL ) {
if( !strcmp(f->d_name, ".") || !strcmp(f->d_name, "..") ){
continue;
}
if( f->d_type == DT_DIR ){
struct stat b;
pathcat(spath, f->d_name);
xstat(spath, &b);
build_dir(spath, dpath, f->d_name, b.st_mode);
*src.end = '\0';
}
}
*src.end = '\0';
*dst.end = '\0';
xclosedir(src.d, spath);
}
int
main(int argc, char **argv)
{
if( argc < 2 ){
errx(EXIT_FAILURE, "src directory missing");
}
if( argc < 3 ){
errx(EXIT_FAILURE, "target directory missing");
}
char spath[PATH_MAX];
char dpath[PATH_MAX];
xrealpath(argv[1], spath);
for( argv += 2; *argv; argv++ ){
char *name = *argv;
if( *name == '/' ){
dpath[0] = '\0';
} else {
getcwd(dpath, sizeof dpath);
}
build_dir(spath, dpath, name, 0777);
}
return 0;
}
static void
xrealpath(const char *s, char *resolved)
{
if( realpath(s, resolved) == NULL ){
err(EXIT_FAILURE, "%s", s);
}
}
static DIR *
xopendir(const char *path)
{
DIR *d = opendir(path);
if( d == NULL ) {
err(EXIT_FAILURE, "%s", path);
}
return d;
}
static void
xstat(const char *path, struct stat *buf)
{
if( stat(path, buf) ){
err(EXIT_FAILURE, "stat %s", path);
}
}
static void
xclosedir(DIR *d, const char *path)
{
if( closedir(d) ) {
err(EXIT_FAILURE, "%s", path);
}
}
static void
pathcat(char *p, char *n)
{
if( snprintf(p, PATH_MAX, "%s/%s", p, n) > PATH_MAX - 1 ){
errx(1, "path too long");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.