'How to move the files from one directory to another in 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.



Solution 1:[1]

use

rename(DestinationFilepath, SourceFilepath);

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

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1