创建目录和列出目录功能最常使用。
获取当前工作目录1. Linux
在shell中我们可以直接输入命令pwd 来显示当前的工作目录
2. C
在C程序中调用getcwd函数可以获取当前的工作目录。
函数声明: char *getcwd(char * buf,size_t size); 当前工作目录存入buf中 #include返回值: 如果目录名超出了参数size长度,函数返回NULL,如果成功,返回buf。 例如: char strpwd[301]; memset(strpwd,0,sizeof(strpwd)); getcwd(strpwd,300); printf("当前目录是:%sn",strpwd);
示例:
void main()
{
char * strpwd=malloc(301);
memset(strpwd,0,sizeof(strpwd));
printf("strpwd=%pn",strpwd);
printf("res=%pn",getcwd(strpwd,300));
printf("当前目录是:%sn",strpwd);
free(strpwd);
}
切换工作目录
1. Linux
在shell中我们可以直接输入命令cd 来显示当前的工作目录
2. C
C中用chdir函数切换目录
函数声明: int chdir(const char *path); 就像我们在shell中使用cd命令切换目录一样,在C程序中使用chdir函数来改变工作目录。 #include返回值:0-切换成功;非0-失败。
示例:
char * strpwd1=malloc(301);
memset(strpwd1,0,sizeof(strpwd1));
chdir("aaa");
getcwd(strpwd1,300);
printf("当前目录是:%sn",strpwd1);
目录的创建和删除
1. Linux
我们可以在shell中可以通过mkdir/rmdir命令来创建/删除目录
2. C
C程序中用mkdir/rmdir函数来创建/删除目录。
创建目录函数的声明: int mkdir(const char *pathname, mode_t mode); #include返回值:创建一个目录,若成功则返回0,否则返回-1 。 mode的含义将按open系统调用的O_CREAT选项中的有关定义设置, 当然,它还要服从umask的设置况,是不是看不明白? 那先固定填0755,注意:0不要省略哦,它表示八进制。 例如: mkdir("/tmp/aaa",0755); // 创建/tmp/aaa目录 删除目录函数的声明: int rmdir(const char *pathname); 返回值:若成功则返回0,否则返回-1
mkdir示例:
#include//ubuntu void main() { mkdir("ddd",0755);//在程序所在文件夹下创建一个名为ddd的文件夹 mkdir("aaa/ddd",0755);//使用这种多文件夹方式创建文件夹时要注意:最后一个文件夹名前面的文件夹都存在,否则就一直返回-1 //已存在的文件再次创建也会报错 }
rmdir示例:
#include获取目录中的文件列表void main() { rmdir("bbb");//如果文件夹中有文件或文件夹也会一直报错,里面是空的才会删除成功。 rmdir("aaa/bbb");//一定要保证aaa存在,否则报错 }
1. Linux
我们可以在shell中可以通过ls命令来获取目录中的文件列表
2. C
#include//包含头文件 相关的库函数(配合使用): 打开目录的函数opendir的声明: DIR *opendir(const char *pathname); 读取目录的函数readdir的声明: struct dirent *readdir(DIR *dirp); 关闭目录的函数closedir的声明: int closedir(DIR *dirp); 目录指针DIR: DIR *目录指针名; readdir读取目录函数返回值为struct dirent结构体: 每调用一次readdir函数会返回一个struct dirent的地址,存放了本次读取到的内容,它的原理与fgets函数读取文件相同。 struct dirent { long d_ino; // inode number 索引节点号 off_t d_off; // offset to this dirent 在目录文件中的偏移 unsigned short d_reclen; // length of this d_name 文件名长 unsigned char d_type; // the type of d_name 文件类型 char d_name [NAME_MAX+1]; // file name文件名,最长255字符 }; 我们只需要关注结构体的d_type和d_name成员,其它的不必关心。 d_name文件名或目录名。 d_type描述了文件的类型,有多种取值,最重要的是8和4,8-常规文件(A regular file);4-目录(A directory),其它的暂时不关心。
获取目录中的文件列表示例:
#include实例#include int main(int argc,char *argv[]) { if (argc != 2) { printf("请指定目录名。n"); return -1; } DIR *dir; // 定义目录指针 // 打开目录 if ( (dir=opendir(argv[1])) == 0 ) return -1; // 用于存放从目录中读取到的文件和目录信息 struct dirent *stdinfo; while (1) { // 读取一条记录并显示到屏幕 if ((stdinfo=readdir(dir)) == 0) break; printf("name=%s,type=%dn",stdinfo->d_name,stdinfo->d_type); } closedir(dir); // 关闭目录指针 }
文件存放在某目录中,该目录下还会有多级子目录,程序员想要的是列出该目录及其子目录下全部的文件名。
例如存在/home/wucz/tmp目录,其子目录结构和文件如下:
实现:
#include#include // 列出目录及子目录下的文件 int ReadDir(const char *strpathname); int main(int argc,char *argv[]) { if (argc != 2) { printf("请指定目录名。n"); return -1; } // 列出目录及子目录下的文件 ReadDir(argv[1]); } // 列出目录及子目录下的文件 int ReadDir(const char *strpathname) { DIR *dir; // 定义目录指针 char strchdpath[256]; // 子目录的全路径 if ( (dir=opendir(strpathname)) == 0 ) return -1; // 打开目录 struct dirent *stdinfo; // 用于存放从目录读取到的文件和目录信息 while (1) { if ((stdinfo=readdir(dir)) == 0) break; // 读取一记录 if (strncmp(stdinfo->d_name,".",1)==0) continue; // 以.开始的文件不读 if (stdinfo->d_type==8) // 如果是文件,显示出来 printf("name=%s/%sn",strpathname,stdinfo->d_name); if (stdinfo->d_type==4) // 如果是目录,再调用一次ReadDir { sprintf(strchdpath,"%s/%s",strpathname,stdinfo->d_name); ReadDir(strchdpath); } } closedir(dir); // 关闭目录指针 }
运行效果:



