C 如何将特定文件传输到目录中?

C 如何将特定文件传输到目录中?,c,C,在这段代码中,我试图复制一个文件并将其粘贴到一个目录中。正如您所见,我也打印了该文件的路径,即子路径1[1],并且打印成功,但当我传输()它时,结果如下,它没有复制。如何将特定文件传输/复制到目录中 给定输出 /home/runner/TestC1/fileName.txt rsync: change_dir "/home/runner/TestC1/fileName.txt" failed: Not a directory (20) rsync error: some fi

在这段代码中,我试图复制一个文件并将其粘贴到一个目录中。正如您所见,我也打印了该文件的路径,即
子路径1[1]
,并且打印成功,但当我
传输()
它时,结果如下,它没有复制。如何将特定文件传输/复制到目录中

给定输出

/home/runner/TestC1/fileName.txt
rsync: change_dir "/home/runner/TestC1/fileName.txt" failed: Not a directory (20)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1196) [sender=3.1.2]
第1部分

int listdir(char *basePath, char files[][SIZE], int *total){
    char path[PATH_MAX];
    struct dirent *dp;
    DIR *dir;

    if (NULL != (dir = opendir(basePath))) {//try to open dir
        while(NULL != (dp = readdir(dir))) {//iterate through contents
            if (strcmp ( dp->d_name, ".") != 0 && strcmp ( dp->d_name, "..") != 0) {
                snprintf(path, SIZE, "%s/%s", basePath, dp->d_name);
                if (dp->d_type == DT_REG) {
                    if (*total < ELEMENTS) {
                        snprintf (files[*total], SIZE, "%s", path);
                        files[*total][SIZE - 1] = 0;
                        ++(*total);
                    }
                }
                if(dp->d_type == DT_DIR){
                    listdir(path, files, total);
                }
            }
        }
        closedir(dir);
    }
    return 0;
}
int listdir(char*basePath,char文件[][SIZE],int*total){
字符路径[path_MAX];
结构方向*dp;
DIR*DIR;
如果(NULL!=(dir=opendir(basePath)){//尝试打开dir
而(NULL!=(dp=readdir(dir)){//遍历内容
如果(strcmp(dp->d_name,“.”)=0和&strcmp(dp->d_name,“…”)!=0){
snprintf(路径,大小,“%s/%s”,基本路径,dp->d_名称);
如果(dp->d_type==DT_REG){
if(*总计<元素){
snprintf(文件[*总数],大小,“%s”,路径);
文件[*total][SIZE-1]=0;
++(*总数);
}
}
如果(dp->d_类型==DT_目录){
listdir(路径、文件、总数);
}
}
}
closedir(dir);
}
返回0;
}
第二部分

char *transfer(char *f1, char *f2){
    char cmd[500];
    snprintf(cmd, 500, "rsync -a %s/ %s", f1, f2);
    system(cmd);

    return 0;
}

int main(){
    char basePath1[SIZE] = "/home/runner/TestC1", pathWords1[ELEMENTS][SIZE], path[ELEMENTS];
    int countPaths1 = 0;

    listdir(basePath1, pathWords1, &countPaths1);
    char subPaths1[5][SIZE];
    for(int i=0; i<5; i++){
        strcpy(subPaths1[i], pathWords1[i]);
    }
    printf("%s\n", subPaths1[1]);
    transfer(subPaths1[1], "/home/runner/TestC1/folder1");

    return 0;
  }
char*传输(char*f1,char*f2){
char-cmd[500];
snprintf(cmd,500,“rsync-a%s/%s”,f1,f2);
系统(cmd);
返回0;
}
int main(){
char basePath1[SIZE]=“/home/runner/TestC1”,pathWords1[ELEMENTS][SIZE],path[ELEMENTS];
int countPaths1=0;
listdir(basePath1、pathWords1和countPaths1);
字符子路径1[5][SIZE];

对于(int i=0;i您可以使用
rename
syscall 例如:

#include <stdio.h> // for rename function
int main() {
    char *old_path = "/home/runner/TestC1/fileName.txt";
    char *new_path = "/home/runner/fileName.txt";
    rename(old_path, new_path);
}
#包含//用于重命名函数
int main(){
char*old_path=“/home/runner/TestC1/fileName.txt”;
char*new_path=“/home/runner/fileName.txt”;
重命名(旧路径、新路径);
}
请看


rename
将文件从dir移动到另一个dir,但如果要复制文件,则应从旧文件中读取并逐字节复制到新文件

它会准确地告诉您,否?它需要一个目录名,但您给它一个文件。打印
cmd
并尝试在控制台中运行相同的命令