linux下目录文件相关函数

作者:LenKernel 和c/c++相关  
最近整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
接下来继续实现相关功能。

相关资料:

linux下目录文件相关函数来源网络,如有侵权请告知,即处理!

编程Tags: