获取当前目录
#include <unistd.h> char *getcwd(char *buf,size_t size); char *get_current_dir_name(void);//注意释放内存 malloc() free()
#include <unistd.h> int chdir(const char *path);//0成功,其他失败(目录不存在或没有权限)
#include<sys/stat.h> int mkdir(const char *pathname,mode_t mode);//pathname-目录名 mode-访问权限,如0755,不要省略0 //0-成功,其他-失败(上级目录不存在或没有权限)
#include<unistd.h> int rmdir(const char *path);
获取目录中文件的列表
#include<dirent.h> DIR *openDir(const char *pathname);//打开目录 struct dirent *readdir(DIR *dirp);//读取目录 int close(DIR *dirp);//关闭目录
struct dirent { #ifndef __USE_FILE_OFFSET64 __ino_t d_ino; __off_t d_off; #else __ino64_t d_ino; __off64_t d_off; #endif unsigned short int d_reclen; unsigned char d_type; //文件类型 8-常规文件 4-目录 char d_name[256]; /* We must not include limits.h! */ }; #ifdef __USE_LARGEFILE64 struct dirent64 { __ino64_t d_ino; __off64_t d_off; unsigned short int d_reclen; unsigned char d_type; char d_name[256]; /* We must not include limits.h! */ }; #endif