Link to home
Create AccountLog in
Avatar of Megha Valishetra
Megha Valishetra

asked on

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

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

Avatar of Megha Valishetra
Megha Valishetra

ASKER

waiting for reply
Avatar of sarabande
did you get any messages from the code?

generally, fgets only would read properly from text files. if you want to copy files, you should open the file in binary mode (add 'b' to file mode) and use fread until file size (which is st_buf.st_size) is reached.

Sara
fopen(spnDirPtr->d_name,"r");

probably the fopen fails as you didn't concatenate the file name with the source folder.

Sara
Yes Sara I got output window showing filename.txt is writing to destination folder and process completed. but when i check destination folder thre are no files
 

I think its just printing lines of printf
Did you check?
did you get valid output from

printf("\n dest file name = %s\n", strDestFileName);

Open in new window


if yes, post Output and check errno after fopen of destination file.

if no, check and post output of

printf("\n Now writing %s file...",spnDirPtr->d_name);

Open in new window


and errno after fopen of source file.

Sara
Now writing . file...
 dest file name = C:\Users\meghav\Desktop\ma/.

 Now writing .. file...
 dest file name = C:\Users\meghav\Desktop\ma/..

 Now writing input.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input.txt

 Now writing input_001_20170330_103501.380.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_001_20170330_103501.380.txt

 Now writing input_002_20170330_103501.381.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_002_20170330_103501.381.txt

 Now writing input_003_20170330_103501.382.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_003_20170330_103501.382.txt

 Now writing input_004_20170330_103501.382.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_004_20170330_103501.382.txt

 Now writing input_005_20170330_103501.383.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_005_20170330_103501.383.txt

 Now writing input_006_20170330_103501.384.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_006_20170330_103501.384.txt

 Now writing input_007_20170330_103501.384.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_007_20170330_103501.384.txt

 Now writing input_008_20170330_103501.385.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_008_20170330_103501.385.txt

 Now writing input_009_20170330_103501.385.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_009_20170330_103501.385.txt

 Now writing input_010_20170330_103501.386.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_010_20170330_103501.386.txt

 Now writing input_011_20170330_103501.386.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_011_20170330_103501.386.txt

 Now writing input_012_20170330_103501.387.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_012_20170330_103501.387.txt

 Now writing input_013_20170330_103501.387.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/input_013_20170330_103501.387.txt

 Now writing output.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output.txt

 Now writing output_001_20170403_175628.451.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_001_20170403_175628.451.txt

 Now writing output_002_20170403_175628.452.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_002_20170403_175628.452.txt

 Now writing output_003_20170403_175628.453.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_003_20170403_175628.453.txt

 Now writing output_004_20170403_175628.455.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_004_20170403_175628.455.txt

 Now writing output_005_20170403_175628.456.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_005_20170403_175628.456.txt

 Now writing output_006_20170403_175628.457.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_006_20170403_175628.457.txt

 Now writing output_007_20170403_175628.458.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_007_20170403_175628.458.txt

 Now writing output_008_20170403_175628.459.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_008_20170403_175628.459.txt

 Now writing output_009_20170403_175628.460.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_009_20170403_175628.460.txt

 Now writing output_010_20170403_175628.461.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_010_20170403_175628.461.txt

 Now writing output_011_20170403_175628.493.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_011_20170403_175628.493.txt

 Now writing output_012_20170403_175628.494.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_012_20170403_175628.494.txt

 Now writing output_013_20170403_175628.495.txt file...
 dest file name = C:\Users\meghav\Desktop\ma/output_013_20170403_175628.495.txt

Process Completed
Press any key to continue . . .


this is the output
but in destination folder there is no file appearing
C:\Users\meghav\Desktop\ma/

you can't mix backslash and slash in path name if using c runtime (fopen, stat). winapi (CreateFile, OpenFile, CopyFile, ..) should be able to handle it. but also shell command with system would fail. if you printf errno after fopen you would get an error which is not 0.

you can find the used kind of slash separator by

char strDestFileName[MAX] = {0};
char cslash = (arDestPath.strchr(arDestPath, '/') == NULL)? '\\' : '/';  // search in folder path whether \ or / is used
sprintf(strDestFileName, "%s%c%s", arDestPath, cslash. spnDirPtr->d_name);

Open in new window


Sara
Slash is not a problem.. I removed it.

Code is not working ... But debuggs correctly
unresolved external symbol _lstat referenced in function

what is this error?
this is unicode (wide character) version of stat function. 'stat' is switched to _lstat if UNICODE is defined in your project. choose 'multi-byte character set' in project properties - configuration properties - general and rebuild project.

Slash is not a problem.. I removed it.

between folder and filename you need to use a slash. so, you can't remove it.


Code is not working ... But debuggs correctly

what is the errno after fopen? i asked already a few times. i can't help if you don't give full Information or post code that doesn't fit to the output.

Sara
#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[] = "C:\\Users\\meghav\\Desktop\\meg";    /*Source directory path*/
	char    arDestPath[] = "C:\\Users\\meghav\\Desktop\\ma";    /*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

errno is just buffer where it contains zero in it
I tried new program see here

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


#define DESTINATION_FOLDER "C:\Users\meghav\Desktop\ma"
#define SOURCE_FOLDER "C:\Users\meghav\Desktop\meg" 



	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("input.txt", "C:\Users\meghav\Desktop\ma\bbb.txt");
		printf("done.\n");
		exit(0);
	}

Open in new window


this is also not working...

check both program please
lstat(entry->d_name, &statbuf);
i told you that lstat is wide-character variant of stat function. if you pass a char buffer to lstat. it will not work as lstat expects a wchar_t buffer.

you really should use multi-byte character set in your project (as described above) or we will need another year until we get the version working.

errno is just buffer where it contains zero in it
errno was set to non-zero if stat function, fopen, fgets, fputs, fread, fwrite, failed.

			if (nErrNo == 0)
				nErrNo = errno;

Open in new window

these statements don't make any sense.

instead:

if (stat(entry->d_name, &statbuf) != 0)
{
    iErrNo = errno;
    printf("\nstat(%s, &statbuf) failed with errno = %d\n", entry->d_name, iErrNo);

Open in new window



do similar after fopen, fgets, fputs.

if fopen or fgets failed the return value is NULL. if fputs failed the return value is EOF (or -1).

Sara
you really should use multi-byte character set in your project (as described above)

I changed but its not working
if (stat(entry->d_name, &statbuf) != 0)
{
    iErrNo = errno;
    printf("\nstat(%s, &statbuf) failed with errno = %d\n", entry->d_name, iErrNo);

where to use these?
 I am not getting
//#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
//#include <SDL.h>
#undef main


#define DESTINATION_FOLDER "C:\Users\meghav\Desktop\ma"
#define SOURCE_FOLDER "C:\Users\meghav\Desktop\meg" 



	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("input.txt", "C:\Users\meghav\Desktop\ma\bbb.txt");
		printf("done.\n");
		exit(0);
	}

Open in new window


Lets concentrate on this program

What is wrong in this? I changed properties but no use
I changed but its not working

of course you must use stat function after you changed project settings to multi-byte.

stat takes const char * as first argument.
lstat takes const wchar_t as first Argument

wchar_t is double-byte what doesn't make sense for file names and file paths since all your names are ASCII.

I am not getting

you have to replace

lstat(entry->d_name, &statbuf)

Open in new window


by

if (stat(entry->d_name, &statbuf) != 0) {
     iErrNo = errno;
     printf("\nstat(%s, &statbuf) failed with errno = %d\n", entry->d_name, iErrNo); 
     return -1;
}

Open in new window


with this you would get if the stat function failed.

same is with fopen:

you have to replace

FILE* pnReadFile = fopen(spnDirPtr->d_name, "r");

Open in new window


by

FILE* pnReadFile = fopen(entry->d_name, "r");
if (pnReadFile == NULL)
{
     iErrNo = errno;
     printf("\nfopen(%s, "r") failed with errno = %d\n", entry->d_name, iErrNo); 
     return -2;
}

Open in new window


with these 2 changes you would see if stat function or fopen failed.


Lets concentrate on this program

no, you repeated a lot of errors we already had corrected in other code, for example the paths don't have \\ as separator but only a single backslash.

Sara
I knw slash thing you only told me .. I ll make it correct.

sorry
of course you must use stat function after you changed project settings to multi-byte.

stat takes const char * as first argument.
lstat takes const wchar_t as first Argument

wchar_t is double-byte what doesn't make sense for file names and file paths since all your names are ASCII.

I replaced . it is debugging correctly .. its printing continuous.... but in destination folder no files are seen
but in destination folder no files are seen

if you were using the last code, there you try a rename of the source folder to the destination folder. that will fail if the destination folder already exists. check the errno after the rename.

you need to copy the file for example by reading it in binary mode into memory and write it to the new file in destination folder from memory.

i posted working code for such copying in some of the threads.

Sara
i posted working code for such copying in some of the threads.

do you know link?
Can you please post a code for transfering files from source folder to destination folder.. please
WAITING FOR REPLY
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ok I got it .. Thank you so much..
I have 1 doubt..
Is it possible to move file to destination.
that means it should not present in source folder after moving...?
is it possible?
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ok thank you :)

one more doubt... Between pc i want transmit...

 Can you help me out?
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
thank you very much...sara