基于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;
}
相关推荐
军训猫猫头16 分钟前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success30 分钟前
【爆刷力扣-数组】二分查找 及 衍生题型
算法
CodeWithMe1 小时前
【Note】《深入理解Linux内核》 Chapter 15 :深入理解 Linux 页缓存
linux·spring·缓存
0wioiw01 小时前
Ubuntu基础(监控重启和查找程序)
linux·服务器·ubuntu
Tipriest_1 小时前
Ubuntu常用的软件格式deb, rpm, dmg, AppImage等打包及使用方法
linux·运维·ubuntu
艾希逐月1 小时前
TCP数据的发送和接收
服务器·网络·tcp/ip
Orlando cron1 小时前
数据结构入门:链表
数据结构·算法·链表
牛客企业服务2 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
胡斌附体2 小时前
linux测试端口是否可被外部访问
linux·运维·服务器·python·测试·端口测试·临时服务器
愚润求学2 小时前
【Linux】自旋锁和读写锁
linux·运维