如何从一个目录中的文件移动到另一个在C? [英] How to move the files from one directory to another in c?

查看:129
本文介绍了如何从一个目录中的文件移动到另一个在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对根据 C 的特定类型。我已作的帮助下创建目录创建子文件夹移动目录中的文件创建项目 POSIX dirent.h 在主目录中具有不同的扩展present文件,但我不知道如何削减从主目录下的文件,并在其特定sub-folder.So粘贴请指导我,我怎么能剪切粘贴从一个目录到另一个文件中的 C

I am creating a project on moving a directory files by creating sub-folders according to their particular type in c.I have made up to creating directories with the help of POSIX library dirent.h for the files having different extension present in the home directory but i don't know how to cut a file from home directory and paste in its particular sub-folder.So please guide me about how can i cut and paste a file from one directory to another in c.

推荐答案

使用

重命名(DestinationFilepath,SourceFilepath);

rename(DestinationFilepath, SourceFilepath);

有关更多信息检查手册页 http://linux.die.net/man/2/重命名

for more info check man page http://linux.die.net/man/2/rename

有关两个不同的系统使用卷曲库。
http://en.wikipedia.org/wiki/CURL

For two different system use cURL library. http://en.wikipedia.org/wiki/CURL

code用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);
}

这篇关于如何从一个目录中的文件移动到另一个在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆