最近整linux下的目录相关操作,笔记日后翻看。
int findindir(string str)
{
DIR * dir;
struct dirent * ent;
dir=opendir(str.c_str());
if(dir==NULL){printf("Open the dir failed.\n");return -1;}
printf("Open success.\n");
while(ent=readdir(dir))
{
struct stat buff;
string *str2=new string(ent->d_name);
string str3=str+'/'+(*str2);
delete str2;
if(stat(str3.c_str(),&buff)<0){printf("Get file information failed. \n");return -1;}
printf("Get file information success.\n");
cout<<"The file name is: ";
printf("%s\n",ent->d_name);
if(S_ISDIR(buff.st_mode))
{
printf("This is a directory.\n In the file :");
printf("The size of it is: %ld\n",buff.st_size);
}
if(S_ISREG(buff.st_mode))
{
printf("This is a ordinary file.\n");
printf("The size of it is: %ld\n",buff.st_size);
}
}
closedir(dir);
return 1;
}
相关头文件:
stat():#include,#include
opendir():#include,#include
接下来继续实现相关功能。
阅读(1174) | 评论(2) | 转发(0) |