Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111907
  • 博文数量: 17
  • 博客积分: 319
  • 博客等级: 入伍新兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-29 09:10
文章分类

全部博文(17)

文章存档

2012年(2)

2011年(15)

分类: C/C++

2011-10-26 14:15:08

opendir(打开目录)   
相关函数 open,readdir,closedir,rewinddir,seekdir,telldir,scandir
 
表头文件 #include
#include
 
定义函数 DIR * opendir(const char * name);
 
函数说明 opendir()用来打开参数name指定的目录,并返回DIR*形态的目录流,和open()类似,接下来对目录的读取和搜索都要使用此返回值。
 
返回值 成功则返回DIR* 型态的目录流,打开失败则返回NULL。
 
错误代码 EACCESS 权限不足
EMFILE 已达到进程可同时打开的文件数上限。
ENFILE 已达到系统可同时打开的文件数上限。
ENOTDIR 参数name非真正的目录
ENOENT 参数name 指定的目录不存在,或是参数name 为一空字符串。
ENOMEM 核心内存不足。
 
readdir(读取目录)   
相关函数 open,opendir,closedir,rewinddir,seekdir,telldir,scandir
 
表头文件 #include
#include
 
定义函数 struct dirent * readdir(DIR * dir);
 函数说明 readdir()返回参数dir目录流的下个目录进入点。
结构dirent定义如下
struct dirent
{
ino_t d_ino;
ff_t d_off;
signed short int d_reclen;
unsigned char d_type;
har d_name[256;
};
d_ino 此目录进入点的inode
d_off 目录文件开头至此目录进入点的位移
d_reclen _name的长度,不包含NULL字符
d_type d_name 所指的文件类型
d_name 文件名
 返回值 成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL。
 附加说明 EBADF参数dir为无效的目录流。
 范例 #include
#include
#include
main()
{
DIR * dir;
struct dirent * ptr;
int i;
dir =opendir(“/etc/rc.d”);
while((ptr = readdir(dir))!=NULL)
{
printf(“d_name: %s\n”,ptr->d_name);
}
closedir(dir);
}
 
执行 d_name:.
d_name:..
d_name:init.d
d_name:rc0.d
d_name:rc1.d
d_name:rc2.d
d_name:rc3.d
d_name:rc4.d
d_name:rc5.d
d_name:rc6.d
d_name:rc
d_name:rc.local
d_name:rc.sysinit
  
readlink(取得符号连接所指的文件)   
相关函数 stat,lstat,symlink
 表头文件 #include
 
定义函数 int readlink(const char * path ,char * buf,size_t bufsiz);
 
函数说明 readlink()会将参数path的符号连接内容存到参数buf所指的内存空间,返回的内容不是以NULL作字符串结尾,但会将字符串的字符数返回。若参数bufsiz小于符号连接的内容长度,过长的内容会被截断。
 
返回值 执行成功则传符号连接所指的文件路径字符串,失败则返回-1,错误代码存于errno。
 
错误代码 EACCESS 取文件时被拒绝,权限不够
EINVAL 参数bufsiz 为负数
EIO I/O 存取错误。
ELOOP 欲打开的文件有过多符号连接问题。
ENAMETOOLONG 参数path的路径名称太长
ENOENT 参数path所指定的文件不存在
ENOMEM 核心内存不足

ENOTDIR 参数path路径中的目录存在但却非真正的目录。

另外 读取一个文件夹里所有文件的方法 还有一个比较原始的,

使用system命令读取文件夹中所有文件名到txt文件,然后从中依次读取文件名

#include

int system() 需要包含stdlib.h 头文件 

system("dir /home/lq/桌面/rcgtest/10.24数据分析/AveResult   >  filename.txt");

将AveResult中的文件读取到filename.txt中 然后可以读取该文件,依次处理文件。

  1. int my_find(const std::string& dirname,
  2.                            const std::string& fileRegular,
  3.                            std::vector& vecFileNames)
  4. {
  5.         DIR *d;
  6.         struct  dirent *drt;
  7.         if ((d = opendir(dirname.c_str())) == NULL)
  8.         {
  9.                 std::cout << "open dir " << dirname << "err" << std::endl;
  10.                 return 1;
  11.         }
  12.         while ((drt = readdir(d)) != NULL)
  13.         {
  14.                 if ((strcmp( drt->d_name, "." ) != 0) && (strcmp( drt->d_name, ".." ) != 0))
  15.                 {
  16.                         char szTempDir[MAX_PATHH] = { 0 };
  17.                         sprintf(szTempDir, "%s/%s", dirname.c_str(), drt->d_name);
  18.                         struct stat buf;
  19.                         stat(szTempDir, &buf);

  20.                         if (S_ISDIR(buf.st_mode))
  21.                         {
  22.                                 //如果是目录,递归调用
  23.                                 my_find(szTempDir, fileRegular, vecFileNames);   
  24.                         }
  25.                         else
  26.                         {   
  27.                                 std::string tmpFileName = szTempDir;
  28.                                 if (tmpFileName.find(fileRegular) != std::string::npos)
  29.                                         vecFileNames.push_back(szTempDir);   
  30.                         }      
  31.                 }
  32.         }
  33.         closedir(d);

  34.         return 0;
  35. }

  36. int main(int argc, char *argv[])
  37. {
  38.         if (argc != 3) return 0;

  39.         std::string findedFiles = argv[2];
  40.         std::vector vecSrcFileNames;
  41.         my_find(argv[1], findedFiles, vecSrcFileNames);
  42.         int fileNum = static_cast(vecSrcFileNames.size());

  43.                
  44.         std::cout << "file number : " << fileNum << std::endl;
  45.         std::vector::iterator it = vecSrcFileNames.begin();
  46.         for (; it != vecSrcFileNames.end(); ++it)
  47.                 std::cout << *it << std::endl;

  48.         return fileNum;
  49. }

 

 

 

#include  
#include  
#include  
#include  
#include  
#include  
#define   MAX_PATH   200
//for   linux
void   findAllFile(char   *   pFilePath)
{
DIR   *   dir;
dirent   *   ptr;
struct   stat   stStatBuf;
char   *p   =   NULL;

if(chdir(pFilePath)==-1)     /*Can   be   used   via   Windows   &   Linux   platform,change   current   dir.   to   some   dir.*/
{
printf( "Your   Inputed   Argument   %s   is   Not   Valid\n ",pFilePath);
exit(1);
}
dir   =   opendir(pFilePath);
while   ((ptr   =   readdir(dir))   !=   NULL)
{
if   (stat(ptr-> d_name,   &stStatBuf)   ==   -1)
{
printf( "Get   the   stat   error   on   file:%s\n ",   ptr-> d_name);
continue;
}
if   ((stStatBuf.st_mode   &   S_IFDIR)   &&   strcmp(ptr-> d_name,   ". ")   !=   0
&&   strcmp(ptr-> d_name,   ".. ")   !=   0)
{
char   Path[MAX_PATH];
strcpy(Path,   pFilePath);
strncat(Path,   "/ ",   1);
strcat(Path,   ptr-> d_name);
findAllFile(Path);
}
if   (stStatBuf.st_mode   &   S_IFREG)
{
//printf( " %s\n ",   ptr-> d_name);
p   =   strstr(ptr-> d_name, ".cpp ");//Modified   here   06-7-27
if   (p   !=   NULL)
printf   ( "%s\n ",   ptr-> d_name);
}

chdir(pFilePath);
}
closedir(dir);
}

int   main(int   argc,   char*   argv[])
{
int   runMode=0;
// findAllFile( "/home/lwf/software ");

if(argc <3)
{
findAllFile(argv[1]);
return   0;
}
else
{
printf( "error ");
return   0;
}

}

#include
#include
#include
using namespace std;
static long total=0;
void ListFile(void)
{
    HANDLE hSearch;
    WIN32_FIND_DATA data;
    hSearch=FindFirstFile("*",&data);
    do{
        if(data.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY
           &&strcmp(data.cFileName,".")
           &&strcmp(data.cFileName,"..")){
            SetCurrentDirectory(data.cFileName);
            ListFile();
            SetCurrentDirectory("..");
        }
        else
   if(strcmp(data.cFileName,".")
   &&strcmp(data.cFileName,"..")){
   cout<   ++total;
  }
    }while(FindNextFile(hSearch,&data));
    FindClose(hSearch);

}
int main()
{
    ListFile();
    cout<<"total "<    system("pause");
    return 0;
}

 

#include
#include
FILE *fp;
void findFile(char filePath[])//这个是你要的函数
{
char szFind[MAX_PATH];//这是要找的
    WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char szFile[MAX_PATH];

    strcpy(szFind,filePath);
    strcat(szFind,"\\*.*");//利用通配符找这个目录下的所以文件,包括目录

hFind=FindFirstFile(szFind,&FindFileData);
    if(INVALID_HANDLE_VALUE == hFind)    return;
   
    while(TRUE)
    {
 
        if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//这是目录
        {
   if(FindFileData.cFileName[0]!='.')//.表示当前目录,因为每个目录下面都有两个默认目录就是..和.分别表示上一级目录和当前目录
   {
    strcpy(szFile,filePath);
    strcat(szFile,"\\");
    strcat(szFile,FindFileData.cFileName);
    findFile(szFile);//寻找这个目录下面的文件
   }
        }
        else
        {
   fprintf(stdout,"%s\\%s\n",filePath,FindFileData.cFileName);//打印出目录下的文件的路径和名称
   fprintf(fp,"%s\\%s\n",filePath,FindFileData.cFileName);//这将结果存档到c:\\path.txt中。
  }
        if(!FindNextFile(hFind,&FindFileData))//寻找下一个文件
   break;
    }
    FindClose(hFind);//关闭句柄
}

int main()
{
fp = fopen("C:\\path.txt","w");
findFile("D:\\e-book\\实习\\随笔\\读书ing");//这里是你要遍历的目录,你自己可以改变,它会显示这个目录下的所有文件,包括这个目录下子目录下的文件。
fclose(fp);
return 0;
}

转载链接:http://hi.baidu.com/xiaoguaiyin/blog/item/5a2e9ca1510bf09747106401.html

阅读(7190) | 评论(0) | 转发(0) |
0

上一篇:get 报文解析

下一篇:用C++遍历目录

给主人留下些什么吧!~~