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

相关推荐
ambition2024224 分钟前
从暴力搜索到理论最优:一道任务调度问题的完整算法演进历程
c语言·数据结构·c++·算法·贪心算法·深度优先
pluvium2724 分钟前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
cmpxr_26 分钟前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
无级程序员29 分钟前
centos7 安装 llvm-toolset-7-clang出错的问题解决
linux·centos
kebeiovo39 分钟前
atomic原子操作实现无锁队列
服务器·c++
Yungoal41 分钟前
常见 时间复杂度计算
c++·算法
6Hzlia1 小时前
【Hot 100 刷题计划】 LeetCode 48. 旋转图像 | C++ 矩阵变换题解
c++·leetcode·矩阵
yashuk1 小时前
C语言实现PAT练习及算法学习笔记,还有SQLite介绍
c语言·sqlite·开源项目·算法学习·pat练习
CHHC18801 小时前
NetCore树莓派桌面应用程序
linux·运维·服务器
ACP广源盛139246256732 小时前
破局 Type‑C 切换器痛点@ACP#GSV6155+LH3828/GSV2221+LH3828 黄金方案
c语言·开发语言·网络·人工智能·嵌入式硬件·计算机外设·电脑