Linux C/C++目录操作

获取当前目录

复制代码
#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

推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
https://xxetb.xet.tech/s/3Zqhgt

相关推荐
orion5721 小时前
Missing Semester Class1:course overview and introduction of shell
linux
apocelipes1 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
郝学胜_神的一滴2 天前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
tntxia3 天前
linux curl命令详解_curl详解
linux
扛枪的书生3 天前
Linux 网络管理器用法速查
linux
见过夏天3 天前
C++ 基础入门完全指南
c++