- 论坛徽章:
- 0
|
这么冷清啊,都没人回。
问题解决了,换种方式实现文件的统计,直接用system调用du命令,是种偷懒的办法,不过一样达到目的,何乐而不为?代码贴上:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
/*copy directory to another directory*/
main(int argc,char **argv)
{
DIR *dp,*dp1;
struct dirent *dirp;
struct stat sizebuf;
const char *dirname;
char tmp[256],tmp1[256];
if(argc!=3) /*判断提交的参数*/
{
printf("Usage is: ./copydir directory1 directory2\n");
exit (1);
}
dp=opendir(argv[1]); /*打开拷贝源目录*/
dp1=opendir(argv[2]);/*打开拷贝目标目录*/
if(dp==NULL)/*如果源目录不存在,则提示没有该目录,并退出*/
{
printf("No such diretory!\n");
exit (1);
}
if(dp1==NULL)/*如果目标目录不存在,提示用户先创建该目录,并退出*/
{
printf("No such diretory!Please create it first\n");
exit (1);
}
if(dp!=NULL)
{
while((dirp=readdir(dp))!=NULL) /*判断目录不为空时,循环拷贝目录下的文件到指定文件夹中*/
{
if(strcmp(dirp->d_name,".")==0||strcmp(dirp->d_name,"..")==0)
continue;
dirname=dirp->d_name;
sprintf(tmp,"cp -dr %s/%s %s",argv[1],dirname,argv[2]);
sprintf(tmp1,"du -ah %s | tee -a cp_info.txt",argv[2]);
system(tmp);
system(tmp1);
}
}
closedir(dp);
closedir(dp1);
} |
|