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

相关推荐
思麟呀11 分钟前
Linux的基础IO流
linux·运维·服务器·开发语言·c++
winner88811 小时前
嵌入式Linux驱动开发全流程:工具协作+核心概念拆解(从入门到理解)
linux·运维·驱动开发
ShiinaKaze1 小时前
fatal error: bits/c++config.h: No such file or directory
linux·gcc·g++
QT 小鲜肉1 小时前
【QT/C++】Qt定时器QTimer类的实现方法详解(超详细)
开发语言·数据库·c++·笔记·qt·学习
Archy_Wang_11 小时前
脚本自动生成专业Linux巡检报告
linux·运维·服务器
WBluuue1 小时前
数据结构与算法:树上倍增与LCA
数据结构·c++·算法
java_logo2 小时前
SGLANG Docker容器化部署指南
linux·运维·docker·容器·eureka·1024程序员节
呆瑜nuage2 小时前
C++之红黑树
c++
杨福瑞2 小时前
数据结构:单链表(2)
c语言·开发语言·数据结构
亮剑20183 小时前
第2节:程序逻辑与控制流——让程序“思考”
开发语言·c++·人工智能