Windows
接口
char* _getcwd( char *buffer, int maxlen );
// 功 能 : 获得当前工作目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回指向buffer的pointer
// 失败返回NULL,且设置errno为以下三个值之一:
// ENODEV 无该设备
// ENOMEM 内存不够
// ERANGE 结果超出范围
// 注 意 : 当第一个参数为 NULL 时, 第二个参数 maxlen 长度设置无效,且函数
// 使用 malloc 分配足够内存, 需要将函数返回值传递给 free() 函数来
// 释放内存. 当第一个参数不为 NULL 时,maxlen 指定长度不够函数返回
// 错,设置errno为ERANGE
int _chdir( const char *dirname );
// 功 能 : 更改当前工作目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno如下:
// ENOENT 该路径不存在
long _findfirst( char *filespec, struct _finddata_t *fileinfo );
// 功 能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext函
// 数后续使用来完成某泛式下的文件遍历.
// 头文件 : #include <io.h>
// 参 数 : filespec - 目标文件规范,可以包含通配符
// fileinfo - 文件信息buffer
// 返回值 : 成功返回唯一的搜索句柄
// 出错返回-1,且设置errno为如下值:
// ENOENT 该泛式无法匹配
// EINVAL 无效文件名
// 注 意 : _finddata_t 说明
struct _finddata_t
{
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[_MAX_FNAME];
};
// 其中 :
// unsigned atrrib : 文件属性的存储位置。它存储一个unsigned单元,用于表示文件的
// 属性。文件属性是用位表示的,主要有以下一些:_A_ARCH(存档)、
// _A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、
// _A_SUBDIR(文件夹)、_A_SYSTEM(系统)。这些都是在<io.h>中
// 定义的宏,可以直接使用,而本身的意义其实是一个无符号整型
// (只不过这个整型应该是2的几次幂,从而保证只有一位为1,而其他
// 位为0)。既然是位表示,那么当一个文件有多个属性时,它往往是
// 通过位或的方式,来得到几个属性的综合。例如只读+隐藏+系统属性,
// 应该为:_A_HIDDEN | _A_RDONLY |_A_SYSTEM 。
// time_t time_create:这里的time_t是一个变量类型,用来存储文件创建时间。
// time_t time_access: 文件最后一次被访问的时间。
// time_t time_write : 文件最后一次被修改的时间。
// _fsize_t size : 文件的大小。这里的_fsize_t应该可以相当于unsigned整型,表示
// 文件的字节数。
// char name[_MAX_FNAME]:文件的文件名。这里的_MAX_FNAME是一个常量宏,它在<stdlib.h>头
// 文件中被定义,表示的是文件名的最大长度。
int _findnext( long handle, struct _finddata_t *fileinfo );
// 功 能 : 按照前面_findfirst中的泛式规则,查找下一个符合该泛式的文件,并以此为依据
// 修改fileinfo中的值
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// fileinfo - 文件信息buffer
// 返回值 : 成功返回0
// 出错返回-1,且设置errno为如下值:
// ENOENT 没有更多的符合该泛式的文件
int _findclose( long handle );
// 功 能 : 关闭搜寻句柄并释放相应资源
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// 返回值 : 成功返回0
// 出错返回-1,且设置errno为如下值:
// ENOENT 没有更多的符合该泛式的文件
int _mkdir( const char *dirname );
// 功 能 : 创建一个新目录,目录名为dirname.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno为以下三个值之一:
// EACCESS 权限不允许
// EEXIST 该目录已存在
// ENOENT 无该文件或目录
int _rmdir( const char *dirname );
// 功 能 : 删除名为dirname的目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno为以下三个值之一:
// EACCESS : 权限不允许
// ENOTEMPTY : dirname不是文件夹;或者该文件夹不空;或
// 者dirname为当前工作文件夹;或者dirname
// 为当根文件夹;
// ENOENT : 无该文件或目录
int _access( const char *path, int mode );
// 功 能 : 测定文件/目录存取权限.
// 头文件 : #include <io.h>
// 参 数 : path - 文件或者目录
// mode - 权限设定,其值如下:
// 00 Existence only
// 02 Write permission
// 04 Read permission
// 06 Read and write permission
int _chdrive( int drive );
// 功 能 : 更改当前工作驱动器.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1
// 注 释 : 参数说明
// drive =1 : A盘
// drive =2 : B盘
// drive =3 : C盘 ...
demo
// 功 能 : 打印目录path中与模式chRE匹配的所有文件明
// 输 入 : path - 待打印的目录
// chRE - 要求匹配的正则表达式
static void printDir( const char* path, const char* chRE )
{
char* chCurPath = getcwd( NULL, 0); // 当前工作目录
printf("current work path: %s\n", chCurPath );
int ret = _chdir( path );
if ( ret < 0 )
{
perror( path );
}
char* newPath = getcwd( NULL, 0 );
printf("new work path: %s\n", newPath);
free(newPath);
struct _finddata_t data;
long hnd = _findfirst( chRE, &data ); // 查找文件名与正则表达式chRE的匹配第一个文件
// 返回唯一的搜索句柄
if ( hnd < 0 )
{
perror( chRE );
}
int nRet = (hnd <0 ) ? -1 : 1;
while ( nRet >= 0 )
{
if ( data.attrib == _A_SUBDIR ) // 如果是目录
printf(" [%s]*\n", data.name );
else
printf(" [%s]\n", data.name );
nRet = _findnext( hnd, &data );
}
_findclose( hnd ); // 关闭当前句柄
chdir( chCurPath); // 切换回之前的工作目录
free( chCurPath );
}
Linux
接口
char * getcwd(char * buf,size_t size);
int chdir(const char *path);
int mkdir(const char * path,mode_t mode);
int rmdir(const char *path);
int closedir(DIR *dpt);
//目录扫描
struct dirent {
ino_t d_ino;
off_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
DIR * opendir(const char *name);
struct dirent *readdir(DIR *dir);
void rewinddir(DIR *dir);
int scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const struct dirent **, const struct dirent **));
int alphasort(const void *a, const void *b);
int versionsort(const void *a, const void *b);
demo
#include <dirent.h>
int main(void)
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist, 0, alphasort);
if (n < 0)
perror("scandir");
else {
while(n--)
{
printf("%sn", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
return 0;
}
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#define MAX_DIR_ENT 1024
typedef int(*qsort_compar)(const void *, const void *);
int hxy_scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const struct dirent **, const struct dirent **))
{
DIR * od;
int n = 0;
struct dirent ** list = NULL;
struct dirent * ent ,* p;
if((dir == NULL) || (namelist == NULL))
return -1;
od = opendir(dir);
if(od == NULL)
return -1;
list = (struct dirent **)malloc(MAX_DIR_ENT*sizeof(struct dirent *));
while(( ent = readdir(od)) != NULL)
{
if(filter!=NULL && !filter(ent))
continue;
p = (struct dirent *)malloc(sizeof(struct dirent));
memcpy((void *)p,(void *)ent,sizeof(struct dirent));
list[n] = p;
n++;
if(n >= MAX_DIR_ENT)
break;
}
closedir(od);
*namelist = realloc((void *)list,n*sizeof(struct dirent *));
if(*namelist == NULL)
*namelist = list;
if(compar)
qsort((void *)*namelist,n,sizeof(struct dirent *),(qsort_compar)compar);
return n;
}
int filter_fn(const struct dirent * ent)
{
if(ent->d_type != DT_REG)
return 0;
return (strncmp(ent->d_name,"lib",3) == 0);
}
typedef int(*scandir_compar)(const struct dirent **, const struct dirent **);
//第二个版本的扫描目录程序,
void scan_lib(const char * dir_name)
{
int n;
struct dirent **namelist;
n = hxy_scandir(dir_name, &namelist, filter_fn, (scandir_compar)alphasort);
if (n < 0)
perror("scandir");
else {
while(n--)
{
printf("%sn", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
int main(void)
{
scan_lib("/usr/lib");
return 0;
}
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
int scan_file(char *dir_name)
{
DIR *dir;
struct dirent *ent;
char *ptr;
dir = opendir(dir_name);
if(dir == NULL)
{
fprintf(stderr,"open directory %s n",dir_name);
return -1;
}
while( (ent = readdir(dir)) != NULL)
{
ptr = strrchr(ent->d_name,'.');
if(ptr && (strcasecmp(ent->d_name, ".txt") == 0))
printf("%sn", ent->d_name);
}
closedir(dir);
}
int main(void)
{
scan_file("/root/Desktop/C/src");
}