
本文共 3700 字,大约阅读时间需要 12 分钟。
文章目录
man getcwd
一、文件夹路径获取
1、学习参考网址:
2、Linux参考手册
1.1 头文件
#include <unistd.h> // Linux系统中#include <direct.h> // windows系统中
1.2 函数原型
1.2.1 getcwd()
char* getcwd(char* buf, size_t size);
描述:该函数的功能是返回当前 工作目录的绝对路径到一个字符数组中(即参数buf)。该路径中不能包含符号链接组件。
返回值是char *的路径
参数char* buf: 返回的目录地址的字符串数组
size_t size: buf数组的大小
注意:如果buf是一个空指针,那么该函数将会出不知名错误;如果给定的size超过了buf实际大小那么,getcwd()函数将会返回一个空指针。若成功获得路径getcwd()返回buf指针;
特别注意的是:当buff是空指针时,getcwd()函数将会使用malloc函数动态的为其分配size(size不为0)大小的内存。若size为0,则getcwd()函数将会为buf分配可能会用到的最大(注意:Linux中文件名字的长度限制是255个英文字符)的内存。使用完返回的路径字符串时,应当记得进行free(buf)操作
1.2.2 get_current_dir_name()
getcwd() 函数使用起来不怎么方便,因为他需要提前设定返回的数组大小,有时候路径长度不变,容易出错。而get_current_dir_name()函数就不需要提前考虑数组的大小。
函数原型:
char* get_current_dir_name(void)
参数说明:该函数直接返回当前工作目录的绝对路径
描述:get_current_dir_name()函数返回的路径也是通过malloc函数动态的分配的内存,因此使用完其返回值后也要记得对返回值进行free()操作。
1.2.3 getwd()
函数原型:
char* getwd(char* buf);
描述:该函数不会使用malloc函数动态的分配内存,但是返回值参数buf的大小必须是一个最少能容纳当前路径的最大可能的长度的字符串指针
1.3 函数使用实例参考
这里只介绍getcwd()和get_current_dir_name()的使用参考代码
1.3.1 getcwd()
#include<stdio.h>#include<stdlib.h>#include<unisted> // 函数所在头文件#include<iostream>using namespace std;int main(){ const int size = 255; char buf[size]; getcwd(buf,size); cout<<"Working path is: "<<buf<<endl; // buf为空指针时 char* buf1 = NULL; buf1 = getcwd(NULL, 0); puts(buf1); // 在屏幕输出该路径 free(buf1); // 使用完路径释放buf1,因为是由getcwd()函数动态分配的内存(malloc) // 此时任然可以查看路径占用了多少字节 cout << "size is: " << sizeof(buf1) << endl; return 0;}
1.3.2 get_current_dir_name()
#include<iostream>#include<unistd.h> // 函数所在头文件using namespace std;int main() { char* buf; buf = get_current_dir_name(); cout << "Current dir name is: " << buf; cout << "size is: " << sizeof(buf) <<endl; free(buf); // 一定记得free,因为是由get_current_dir_name()函数动态分配的内存 return 0;}
二、获取某一存在目录中所有文件名字(包括扩展名)
1、学习参考网站:
2、 Linux的man帮助命令
man opendir
2.1 实现步骤
在Linux系统中读取指定目录下所有文件名的实现步骤主要分三步:
- 打开文件目录opendir()
- 读取文件信息readdir()
- 关闭文件目录closedir()
2.2 包含头文件
#include <sys/types.h>#include <dirent.h>
2.3 打开文件目录opendir
相关函数原型:
DIR *opendir(const char *name);DIR *fdopendir(int fd);
描述:
- opendir() 函数根据目录路径打开一个目录流,并返回一个目录流()指针。这个指针指向目录的首地址。
- fdopendir()函数和opendir()函数类似,只是它是根据文件操作流fd(即常用的文件读取),打开的目录,然后同样是返回目录流指针。
#include <iostream>#include <sys/types.h>#include <dirent.h>using namespace std;int main(){ // 1、打开文件目录 DIR* dirStream; const char* path = "/home/XX/code/"; dirStream = opendir(path); // 2、接下来是读取文件信息,看2.4节 return 0;}
2.4 读取文件信息
相关函数原型:
#include <dirent.h> // 所在头文件struct dirent *readdir(DIR *dirp);
函数描述:该函数根据打开的目录流指针dirp,返回一个指针。如果在目录流的结尾,那么该指针为空。否则就是表示由dirp指针指向的目录流的下一个目录条目
struct dirent { __ino_t d_ino; /*inode number */ // 节点号索引 __off_t d_off; /*offset to this dirent */ // 在目录文件中的偏移 unsigned short int d_reclen; /*length of this d_name */ // 这个文件的名字长度 unsigned char d_type; /*the type of d_name */ // 文件类型 char d_name[256]; /*file name(null-terminated) */ // 这个是文件名 };————————————————
#include <iostream>#include <sys/types.h>#include <dirent.h>#include <string>#include <vector>using namespace std;int main(){ // 1、打开文件目录 DIR* dirStream; const char* path = "/home/XX/code/"; dirStream = opendir(path); // 2、接下来是读取文件信息 struct dirent* dirInfo; vector<string> name; while((dirInfo = readdir(dirStream))!=0); { cout << "Value test: "<<dirInfo -> d_name << endl; name.push_back(dirInfo -> d_name); } // 注意此时dirStream 已经指向文件尾了 // 3、最后关闭文件目录 closedir(dirStream); return 0;}
2.4 关闭文件目录
和opendir()成对出现:int closedir(DIR *)。略过
三、其他关于目录操作的函数
函数原型如下
#include <sys/types.h>#include <dirent.h>long telldir(DIR *dirp); //返回与目录流dirp相关联的当前位置(地址)void seekdir(DIR *dirp, long loc);// 设置目录流dirp指向loc,其中参数loc必须是telldir()函数返回的地址。设置之后下次调用readdir函数将从该位置开始读void rewinddir(DIR *dirp); // 该函数是将目录流指针dirp重置到开始的位置(地址)
发表评论
最新留言
关于作者
