基于linux下的高并发服务器开发(第一章)- 目录操作函数

09 / 目录操作函数

(1)int mkdir(const char* pathname,mode_t mode);

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
作用:创建一个目录
参数:
pathname: 创建的目录的路径
mode: 权限,八进制的数
返回值:
成功返回0, 失败返回-1

cpp 复制代码
/*
    #include <sys/stat.h>
    #include <sys/types.h>
    int mkdir(const char *pathname, mode_t mode);
        作用:创建一个目录
        参数:
            pathname: 创建的目录的路径
            mode: 权限,八进制的数
        返回值:
            成功返回0, 失败返回-1
*/

#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>

int main() {

    int ret = mkdir("aaa", 0777);

    if(ret == -1) {
        perror("mkdir");
        return -1;
    }

    return 0;
}

(1)gcc mkdir.c -o mkdir

(2)./mkdir

(2)int rename(const char *oldpath, const char *newpath);

cpp 复制代码
/*
    #include <stdio.h>
    int rename(const char *oldpath, const char *newpath);

*/
#include <stdio.h>

int main() {

    int ret = rename("aaa", "bbb");

    if(ret == -1) {
        perror("rename");
        return -1;
    }

    return 0;
}

(3)int chdir(const char *path);

char *getcwd(char *buf, size_t size);

#include <unistd.h>
int chdir(const char *path);
作用:修改进程的工作目录,比如在/home/nowcoder 启动了一个可执行程序a.out,

进程的工作目录 /home/nowcoder
参数:
path : 需要修改的工作目录

**#include <unistd.h>
char *getcwd(char *buf, size_t size);
作用:获取当前工作目录
参数:

  • buf : 存储的路径,指向的是一个数组(传出参数)
  • size: 数组的大小
    返回值:
    返回的指向的一块内存,这个数据就是第一个参数**
cpp 复制代码
/*

    #include <unistd.h>
    int chdir(const char *path);
        作用:修改进程的工作目录
            比如在/home/nowcoder 启动了一个可执行程序a.out, 进程的工作目录 /home/nowcoder
        参数:
            path : 需要修改的工作目录

    #include <unistd.h>
    char *getcwd(char *buf, size_t size);
        作用:获取当前工作目录
        参数:
            - buf : 存储的路径,指向的是一个数组(传出参数)
            - size: 数组的大小
        返回值:
            返回的指向的一块内存,这个数据就是第一个参数

*/
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

int main() {

    // 获取当前的工作目录
    char buf[128];
    getcwd(buf, sizeof(buf));
    printf("当前的工作目录是:%s\n", buf);

    // 修改工作目录
    int ret = chdir("/home/nowcoder/Linux/lesson13");
    if(ret == -1) {
        perror("chdir");
        return -1;
    } 

    // 创建一个新的文件
    int fd = open("chdir.txt", O_CREAT | O_RDWR, 0664);
    if(fd == -1) {
        perror("open");
        return -1;
    }

    close(fd);

    // 获取当前的工作目录
    char buf1[128];
    getcwd(buf1, sizeof(buf1));
    printf("当前的工作目录是:%s\n", buf1);
    
    return 0;
}
相关推荐
KoiHeng41 分钟前
部分排序算法的Java模拟实现(复习向,非0基础)
java·算法·排序算法
deeper_wind4 小时前
keeplived双击热备配置
linux·运维·网络
GoldKey6 小时前
gcc 源码阅读---语法树
linux·前端·windows
不易思不逸7 小时前
Ubuntu20.04 RTX 4080 Nvidia驱动安装
运维·服务器
艾莉丝努力练剑8 小时前
【数据结构与算法】数据结构初阶:详解顺序表和链表(四)——单链表(下)
c语言·开发语言·数据结构·学习·算法·链表
黎茗Dawn8 小时前
连接new服务器注意事项
linux·python
m0_694845578 小时前
云服务器如何管理数据库(MySQL/MongoDB)?
服务器·数据库·mysql
L_autinue_Star9 小时前
从0到1实现Shell!Linux进程程序替换详解
linux·运维·服务器·c++·chrome
yngsqq9 小时前
移动碰撞法 ——套料排版算法——CAD c#
算法
程序员JerrySUN10 小时前
Linux 文件系统实现层详解:原理、结构与驱动衔接
android·linux·运维·数据库·redis·嵌入式硬件