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

相关推荐
郭涤生4 小时前
布隆过滤器
c++
徐子元竟然被占了!!4 小时前
Linux-systemctl
linux·数据库·oracle
喵了meme4 小时前
C语言实战4
c语言·开发语言
智者知已应修善业4 小时前
【求中位数】2024-1-23
c语言·c++·经验分享·笔记·算法
9ilk4 小时前
【C++】--- 特殊类设计
开发语言·c++·后端
_w_z_j_7 小时前
Linux----mmap
linux
程序员zgh8 小时前
Linux系统常用命令集合
linux·运维·服务器·c语言·开发语言·c++
獭.獭.8 小时前
C++ -- STL【unordered_set与unordered_map的实现】
开发语言·c++·unordered_map·unordered_set
Bigan(安)8 小时前
【奶茶Beta专项】【LVGL9.4源码分析】09-core-obj_class对象类系统
linux·c语言·mcu·arm·unix
qq_433554549 小时前
C++数位DP
c++·算法·图论