linux目录操作函数

mkdir函数

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

int main(){

    int ret = mkdir("aaa",0777);//最终的权限mode & (~umask)
    //目录必须要有可执行权限才能进入

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

rename函数

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

int main(){

    int ret = rename("aaa","bbb");//最终的权限mode & (~umask)
    //目录必须要有可执行权限才能进入

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

chdir函数和getcwd函数

// #include <unistd.h>

// int chdir(const char *path);//修改进程的工作目录
//     //在某个目录下执行程序的时候,该程序的工作目录就是前面的目录

// #include <unistd.h>

// char *getcwd(char *buf, size_t size);//buf 是一个传出参数

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
    char buf [128];
    getcwd(buf, sizeof(buf));
    printf("当前的工作目录:%s\n",buf);

    int ret = chdir("/home/gabe/Linux/lesson13");
    if(ret == -1){
        perror("chdir");
    }
    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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值