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

相关推荐
Pacify_The_North6 分钟前
【进程控制二】进程替换和bash解释器
linux·c语言·开发语言·算法·ubuntu·centos·bash
superior tigre14 分钟前
C++学习:六个月从基础到就业——C++20:协程(Coroutines)
c++·学习·c++20
ephemerals__17 分钟前
【Linux】简易版Shell实现(附源码)
linux·运维·chrome
superior tigre19 分钟前
C++学习:六个月从基础到就业——C++20:概念(Concepts)
c++·学习·c++20
JeffersonZU19 分钟前
【shell】shell和shell脚本简介
linux·ubuntu·bash
wuqingshun31415932 分钟前
经典算法 (A/B) mod C
c语言·开发语言·c++·算法·蓝桥杯
半青年37 分钟前
Qt图表库推荐指南与分析
c语言·开发语言·javascript·c++·qt·信息可视化
姬公子52144 分钟前
leetcodehot100刷题——排序算法总结
数据结构·c++·算法·排序算法
烦躁的大鼻嘎1 小时前
【Linux】ELF与动静态库的“暗黑兵法”:程序是如何跑起来的?
linux·运维·服务器·c++·vscode·ubuntu
ShineSpark1 小时前
C++面试2——C与C++的关系
c语言·c++·面试