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
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;
}
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
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
ASKER
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
I think its just printing lines of printf
ASKER
Did you check?
did you get valid output from
if yes, post Output and check errno after fopen of destination file.
if no, check and post output of
and errno after fopen of source file.
Sara
printf("\n dest file name = %s\n", strDestFileName);
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);
and errno after fopen of source file.
Sara
ASKER
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.tx t
Now writing output_001_20170403_175628 .451.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 1_20170403 _175628.45 1.txt
Now writing output_002_20170403_175628 .452.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 2_20170403 _175628.45 2.txt
Now writing output_003_20170403_175628 .453.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 3_20170403 _175628.45 3.txt
Now writing output_004_20170403_175628 .455.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 4_20170403 _175628.45 5.txt
Now writing output_005_20170403_175628 .456.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 5_20170403 _175628.45 6.txt
Now writing output_006_20170403_175628 .457.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 6_20170403 _175628.45 7.txt
Now writing output_007_20170403_175628 .458.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 7_20170403 _175628.45 8.txt
Now writing output_008_20170403_175628 .459.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 8_20170403 _175628.45 9.txt
Now writing output_009_20170403_175628 .460.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_00 9_20170403 _175628.46 0.txt
Now writing output_010_20170403_175628 .461.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_01 0_20170403 _175628.46 1.txt
Now writing output_011_20170403_175628 .493.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_01 1_20170403 _175628.49 3.txt
Now writing output_012_20170403_175628 .494.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_01 2_20170403 _175628.49 4.txt
Now writing output_013_20170403_175628 .495.txt file...
dest file name = C:\Users\meghav\Desktop\ma /output_01 3_20170403 _175628.49 5.txt
Process Completed
Press any key to continue . . .
this is the output
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
Now writing input_001_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_002_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_003_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_004_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_005_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_006_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_007_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_008_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_009_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_010_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_011_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_012_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing input_013_20170330_103501.
dest file name = C:\Users\meghav\Desktop\ma
Now writing output.txt file...
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_001_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_002_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_003_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_004_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_005_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_006_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_007_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_008_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_009_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_010_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_011_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_012_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Now writing output_013_20170403_175628
dest file name = C:\Users\meghav\Desktop\ma
Process Completed
Press any key to continue . . .
this is the output
ASKER
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);
Sara
ASKER
Slash is not a problem.. I removed it.
Code is not working ... But debuggs correctly
Code is not working ... But debuggs correctly
ASKER
unresolved external symbol _lstat referenced in function
what is this error?
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.
between folder and filename you need to use a slash. so, you can't remove it.
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
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
ASKER
#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;
}
ASKER
errno is just buffer where it contains zero in it
ASKER
I tried new program see here
this is also not working...
check both program please
//#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);
}
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 iterrno was set to non-zero if stat function, fopen, fgets, fputs, fread, fwrite, failed.
if (nErrNo == 0)
nErrNo = errno;
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);
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
ASKER
you really should use multi-byte character set in your project (as described above)
I changed but its not working
ASKER
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
ASKER
//#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);
}
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)
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;
}
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");
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;
}
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
ASKER
I knw slash thing you only told me .. I ll make it correct.
sorry
sorry
ASKER
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
ASKER
i posted working code for such copying in some of the threads.
do you know link?
ASKER
Can you please post a code for transfering files from source folder to destination folder.. please
ASKER
WAITING FOR REPLY
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok I got it .. Thank you so much..
ASKER
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?
Is it possible to move file to destination.
that means it should not present in source folder after moving...?
is it possible?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok thank you :)
one more doubt... Between pc i want transmit...
Can you help me out?
one more doubt... Between pc i want transmit...
Can you help me out?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you very much...sara
ASKER