我想将特定文件夹中的所有文件复制到Linux上使用C的其他文件夹。
它可以工作;但是,目标文件夹的文件少于源文件夹。
我不知道自己错了什么。任何人都可以帮我解决这个问题吗?
这是我的代码:
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
if (argc!=3)
{
printf("USAGE: programname source destination\n");
return 1;
}
DIR *srcPtr;
if (mkdir(argv[2],0777)<0)
{
printf("Unable to create directory %s (probably it already exists)\n",argv[2]);
}
srcPtr=opendir(argv[1]);
if (srcPtr==NULL)
{
printf("BAD OPENDIR %s\n",argv[1]);
return 1;
}
struct dirent *dirPtr;
struct stat statbuff;
int fdSrc,fdDest;
char *path_copia;
unsigned int len_path_copia;
unsigned char c;
chdir(argv[1]);
while ( (dirPtr=readdir(srcPtr)) !=NULL)
{
if (lstat(dirPtr->d_name,&statbuff)<0)
{
printf("BAD LSTAT\n");
return 1;
}
if (S_ISREG(statbuff.st_mode))
//IT's A REGULAR FILE WITH EXECUTION rights for the owner
{
//COPY----
len_path_copia=strlen(argv[2]);
len_path_copia+=1+strlen(dirPtr->d_name);
path_copia=(char *)malloc(len_path_copia*sizeof(char));
strcpy(path_copia,argv[2]);
strcat(path_copia,dirPtr->d_name);
printf("Copying %s in %s\n",dirPtr->d_name,path_copia);
if ( (fdDest=open(path_copia,O_CREAT|O_TRUNC|O_WRONLY,(statbuff.st_mode &S_IRWXU)|(statbuff.st_mode &S_IRWXG)|(statbuff.st_mode &S_IRWXO) ) ) <0 )
{
printf("BAD OPEN %s\n",path_copia);
return 1;
}
if ( (fdSrc=open(dirPtr->d_name,O_RDONLY))<0 )
{
printf("BAD OPEN %s\n",dirPtr->d_name);
return 1;
}
while (read(fdSrc,&c,1)==1)
{
write(fdDest,&c,1);
}
close(fdSrc);
close(fdDest);
free(path_copia);
}
}
chdir("..");
closedir (srcPtr);
return 0;
}
答案 0 :(得分:0)
首先,我想指出该计划确实值得怀疑。我认为还有更多经过良好测试的替代品(rsync?),所以我只能假设您编写了这个程序来学习c。在这种情况下,需要学习很多东西,但这是一个很好的第一个成就:)...
无论如何,我编译了你的代码并发现了一些问题:
对于导致文件较少的问题,我认为问题是因为您认为它应该写入./argv [2],它实际写入./argv [1] / argv [2] /。
修复后的最终结果:
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
if (argc!=3)
{
printf("USAGE: programname source destination\n");
return 1;
}
DIR *srcPtr;
if (mkdir(argv[2],0777)<0)
{
printf("Unable to create directory %s (probably it already exists)\n",argv[2]);
}
srcPtr=opendir(argv[1]);
if (srcPtr==NULL)
{
printf("BAD OPENDIR %s\n",argv[1]);
return 1;
}
struct dirent *dirPtr;
struct stat statbuff;
int fdSrc,fdDest;
char *path_copia;
char *sourceFileName;
unsigned int len_path_copia;
unsigned int len_sourceFileName;
unsigned char c;
// chdir(argv[1]);
while ( (dirPtr=readdir(srcPtr)) !=NULL)
{
len_sourceFileName = strlen(argv[1]);
len_sourceFileName += 1+strlen(dirPtr->d_name);
sourceFileName = malloc(len_sourceFileName*sizeof(char));
strcpy(sourceFileName,argv[1]);
strcat(sourceFileName,dirPtr->d_name);
if (lstat(sourceFileName,&statbuff)<0)
{
printf("BAD LSTAT (%s)\n", sourceFileName);
return 1;
}
if (S_ISREG(statbuff.st_mode))
//IT's A REGULAR FILE WITH EXECUTION rights for the owner
{
//COPY----
len_path_copia=strlen(argv[2]);
len_path_copia+=1+strlen(dirPtr->d_name);
path_copia=(char *)malloc(len_path_copia*sizeof(char));
strcpy(path_copia,argv[2]);
strcat(path_copia,dirPtr->d_name);
printf("Copying %s in %s\n",sourceFileName,path_copia);
if ( (fdDest=open(path_copia,O_CREAT|O_TRUNC|O_WRONLY,(statbuff.st_mode &S_IRWXU)|(statbuff.st_mode &S_IRWXG)|(statbuff.st_mode &S_IRWXO) ) ) <0 )
{
printf("BAD OPEN %s\n",path_copia);
return 1;
}
if ( (fdSrc=open(sourceFileName,O_RDONLY))<0 )
{
printf("BAD OPEN %s\n", sourceFileName);
return 1;
}
while (read(fdSrc,&c,1)==1)
{
write(fdDest,&c,1);
}
close(fdSrc);
close(fdDest);
free(path_copia);
}
}
// chdir("..");
closedir (srcPtr);
return 0;
}