We help IT Professionals succeed at work.
Get Started

I want to transfer a file from source folder to destination folder.

Megha Valishetra
on
339 Views
Last Modified: 2017-04-07
I am trying to transfer files from folder to folder .
but its not transfering files.
i dont know where i am doing mistake
#include<stdio.h>
#include<stdio.h>
#include<dirent.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#define MAX 1024

int main()
{
    char    arSrcPath[]    = "/home/mpe4/Src";    /*Source directory path*/
    char    arDestPath[]    = "/home/mpe4/Dest";    /*dest directory path*/
    struct    dirent* spnDirPtr;    /* struct dirent to store all files*/

    DIR* pnOpenDir = NULL;    /*DIR Pointer to open Dir*/
    DIR* pnReadDir = NULL;    /*DIR POinter to read directory*/

    pnOpenDir = opendir(arSrcPath); 

    if(!pnOpenDir)
    printf("\n ERROR! Directory can not be open");

    else
    {    
    int nErrNo = 0;
    while(spnDirPtr = readdir(pnOpenDir))
    {
        if(nErrNo == 0)
        nErrNo = errno;
        printf("\n Now writing %s file...",spnDirPtr->d_name);

        printf("\n dest file name = %s/%s\n", arDestPath, spnDirPtr->d_name);

        struct stat st_buf;
        stat(spnDirPtr->d_name, &st_buf);
        if (S_ISDIR (st_buf.st_mode))
        {
            continue;
        }
        else if (S_ISREG (st_buf.st_mode))
        {
            FILE* pnReadFile = fopen(spnDirPtr->d_name,"r");

            if(pnReadFile)
            {
                printf("\n Now reading %s file...",spnDirPtr->d_name);

                char strDestFileName[MAX] = {0};
                sprintf(strDestFileName, "%s/%s", arDestPath, spnDirPtr->d_name);
                printf("\n dest file name = %s\n", strDestFileName);

                FILE* pnWriteFile  = fopen(strDestFileName, "w");    /*File Pointer to write in file*/
                if(pnWriteFile)
                {
                    char buffer[MAX] = {0};    /*Buffer to store files content*/

                        while(fgets(buffer, MAX, pnReadFile))
                        {
                            fputs(buffer, pnWriteFile);
                        }
                    fclose(pnWriteFile);
                }
            else
            {
                printf("\n Unable to open file %s", strDestFileName);
            }
            fclose(pnReadFile);
    }
    else
    {
        printf ("\nERROR! File Could not be open for reading");
    }
    }
    }
    if(nErrNo != errno)
        printf ("\nERROR Occurred!\n");
    else
        printf ("\nProcess Completed\n");

    }
    closedir(pnOpenDir);
    return 0;
}   

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2016
Commented:
This problem has been solved!
Unlock 5 Answers and 35 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE