#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;
}
fopen(spnDirPtr->d_name,"r");
printf("\n dest file name = %s\n", strDestFileName);
printf("\n Now writing %s file...",spnDirPtr->d_name);
C:\Users\meghav\Desktop\ma/
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);
Slash is not a problem.. I removed it.
Code is not working ... But debuggs correctly
#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;
}
//#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);
}
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.
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.if (stat(entry->d_name, &statbuf) != 0)
{
iErrNo = errno;
printf("\nstat(%s, &statbuf) failed with errno = %d\n", entry->d_name, iErrNo);
you really should use multi-byte character set in your project (as described above)
if (stat(entry->d_name, &statbuf) != 0)
{
iErrNo = errno;
printf("\nstat(%s, &statbuf) failed with errno = %d\n", entry->d_name, iErrNo);
//#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);
}
I changed but its not working
I am not getting
lstat(entry->d_name, &statbuf)
if (stat(entry->d_name, &statbuf) != 0) {
iErrNo = errno;
printf("\nstat(%s, &statbuf) failed with errno = %d\n", entry->d_name, iErrNo);
return -1;
}
FILE* pnReadFile = fopen(spnDirPtr->d_name, "r");
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;
}
Lets concentrate on this program
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.
but in destination folder no files are seen
i posted working code for such copying in some of the threads.