Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c中将文件从一个目录移动到另一个目录?_C_File Io - Fatal编程技术网

如何在c中将文件从一个目录移动到另一个目录?

如何在c中将文件从一个目录移动到另一个目录?,c,file-io,C,File Io,我正在创建一个项目,通过在c中根据特定类型创建子文件夹来移动目录文件。我已经在POSIXlibrarydirent.h的帮助下创建目录,用于主目录中具有不同扩展名的文件,但我不知道如何从中剪切文件主目录并粘贴到其特定的子文件夹中。因此,请指导我如何使用 重命名(DestinationFilepath、SourceFilepath) 有关更多信息,请查看手册页 对于两个不同的系统,请使用cURL库。 C代码 #include <unistd.h> #include <stdio

我正在创建一个项目,通过在
c
中根据特定类型创建子文件夹来移动目录文件。我已经在
POSIX
library
dirent.h
的帮助下创建目录,用于主目录中具有不同扩展名的文件,但我不知道如何从中剪切文件主目录并粘贴到其特定的子文件夹中。因此,请指导我如何使用

重命名(DestinationFilepath、SourceFilepath)

有关更多信息,请查看手册页

对于两个不同的系统,请使用cURL库。

C代码

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

#define DESTINATION_FOLDER "/home/second/"
#define SOURCE_FOLDER "/home/first/" 

void printdir()
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    struct tm      *tm;

    char src_folder[1024];
    char dest_folder[1024];


    if((dp = opendir(SOURCE_FOLDER)) == NULL) {
         fprintf(stderr,"cannot open directory: %s\n", SOURCE_FOLDER);
         return;
    }
    chdir(SOURCE_FOLDER);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name,&statbuf);

        if(!S_ISDIR(statbuf.st_mode)) \
        {
             sprintf(src_folder,"%s%s", SOURCE_FOLDER,entry->d_name); 
             sprintf(dest_folder,"%s%s", DESTINATION_FOLDER,entry->d_name); 
             printf("%s----------------%s\n",entry->d_name,dest_folder);
             rename(src_folder, dest_folder);
        }
    }
    chdir("..");
    closedir(dp);
}

int main()
{
    while(1)
    {
        printdir();
     }
    rename("aaa.txt", "/home/second/bbb.txt");
    printf("done.\n");
    exit(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义目标文件夹“/home/second/”
#定义源文件夹“/home/first/”
void printdir()
{
DIR*dp;
结构方向*条目;
结构stat statbuf;
struct tm*tm;
char src_文件夹[1024];
char dest_文件夹[1024];
if((dp=opendir(SOURCE_FOLDER))==NULL){
fprintf(stderr,“无法打开目录:%s\n”,源文件夹);
返回;
}
chdir(SOURCE_文件夹);
while((entry=readdir(dp))!=NULL){
lstat(输入->数据单位名称和状态单位);
如果(!S_ISDIR(statbuf.st_模式))\
{
sprintf(src_文件夹,“%s%s”,源_文件夹,条目->d_名称);
sprintf(目标文件夹,“%s%s”,目标文件夹,条目->数据单元名称);
printf(“%s------------------%s\n”,条目->数据单元名称,目的文件夹);
重命名(src_文件夹、dest_文件夹);
}
}
chdir(“…”);
closedir(dp);
}
int main()
{
而(1)
{
printdir();
}
重命名(“aaa.txt”,“home/second/bbb.txt”);
printf(“完成。\n”);
出口(0);
}

只有当源和目标位于同一分区上时,这才有效。@有人请给我一些lstat()函数的参考。请参考,希望这能解决问题