0708,LINUX目录相关操作

主要是冷气太足感冒了,加上少吃药抗药性差,全天昏迷,学傻了学傻了

cat t_chdir.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    // ./t_chdir dir 
    if(argc!=2){
        error(1,errno,"Usage:%s dir",argv[0]);
    }
    //打印当前工作目录
    char buf[128];
    printf("%s\n",getcwd(buf,128));

    //改变当前工作目录
    if(chdir(argv[1])==-1){
        error(1,errno,"chdir %s ",argv[1]);
    }

    printf("%s\n",getcwd(buf,128));
    return 0;
}

cat t_getcwd.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>

int main(int argc,char *argv[])
{
    // ./t_getcwd
    //char buf[128];
    //char *cwd=getcwd(buf,128);

    char* cwd=getcwd(NULL,0);
    //如果传入的 buf为 NULL,且size为0
    //,则 getcwd 会调用 malloc 申请合适大小的内存空间,
    //填入当前工作目录的绝对路径
    //,然后返回alloc申请的空间的地址。
    if(!cwd){
        error(1,errno,"getcwd");
    }
    printf("%s\n",cwd);
    return 0;
}

cat t_mkdir.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    // ./t_mkfir dir mode
    if(argc!=3){
        error(1,errno,"Usage:%s dir mode",argv[0]);
    }
    mode_t mode;
    sscanf(argv[2],"%o",&mode);
    
    if(mkdir(argv[1],mode)==-1){
        error(1,errno,"mkdir %s ",argv[1]);
    }

    return 0;
}

cat t_readdir.c

cpp 复制代码
#include <func.h>


int main(int argc,char *argv[])
{
    // ./t_readdir dir 
    if(argc!=2){
        error(1,0,"Usage:%s dir mode",argv[0]);
    }
    //打开目录流
    DIR * dirp=opendir(argv[1]);
    if(dirp==NULL){
        error(1,errno,"opendir :%s ",argv[1]);
    }

    //依次读取每一个目录流
    errno=0;
    struct dirent *p;
    while((p=readdir(dirp))!=NULL){
        //打印目录项
        printf("d_ino=%ld   ,d_off=%ld   ,d_reclen=%hu  ,d_type=%u   ,d_name=%s\n",
               p->d_ino,
               p->d_off,
               p->d_reclen,
               p->d_type,
               p->d_name );
    }//p==NULL
    if(errno){
        error(0,errno,"readdir");
    }
    //关闭目录流
    closedir(dirp);
    return 0;
}

cat t_rmdir.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    // ./t_rmfir dir
    if(argc!=2){
        error(1,errno,"Usage:%s dir mode",argv[0]);
    }


    
    if(rmdir(argv[1])==-1){
        error(1,errno,"rmdir %s ",argv[1]);
    }

    return 0;
}
相关推荐
回到原点的码农6 小时前
Spring Data JDBC 详解
java·数据库·spring
Shi_haoliu6 小时前
openClaw源码部署-linux
前端·python·ai·openclaw
gf13211116 小时前
python_查询并删除飞书多维表格中的记录
java·python·飞书
zb200641206 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot
·醉挽清风·6 小时前
学习笔记—Linux—文件IO
linux·服务器·学习
程序员小寒6 小时前
前端性能优化之白屏、卡顿指标和网络环境采集篇
前端·javascript·网络·性能优化
一勺菠萝丶6 小时前
Flowable + Spring 集成踩坑:流程结束监听器查询历史任务为空 & 获取不到审批意见
java·数据库·spring
jwn9996 小时前
Spring Boot 整合 Keycloak
java·spring boot·后端
宁波阿成6 小时前
OpenClaw 在 Ubuntu 22.04.5 LTS 上的安装与问题处理记录
java·linux·ubuntu·openclaw·龙虾
mldlds6 小时前
SpringBoot详解
java·spring boot·后端