Linux 部分IO函数

open、close函数

标准C库IO函数的说明文档在 man 3 open

Linux系统IO函数说明文档在man 2 open

open()函数打开文件 && errno

例子:目录下没有a.txt那么运行的时候会报错

open创建新文件

本来没有 create.txt 运行完有了 ll可看文件的权限 我们输入的0777 umask改成了0775,抹去了其他组写的权限

close函数

close函数就是关闭对应的文件描述符

read write函数

lseek函数

stat、lstat函数

Linux输入stat指令获得文件属性

更改时间是里面的内容改了 最近改动是文件的权限啥的改了

ln -s创建一个软链接的文件

举例自己得到文件的属性

cpp 复制代码
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include<time.h>
#include<string.h>
//模拟实现ls -l指令
//-rw-rw-r--  1 xiaowu xiaowu   13 9月  11 14:00 a.txt
int main(int argc, char* argv[]) {
    //判断输入的参数是否正确
    if(argc < 2) {
        printf("%s  filname\n", argv[0]);
        return -1;
    }
    //通过stat函数获取用户传入的文件的信息
    struct stat st;
    int ret = stat(argv[1], &st);
    if(ret == -1){
        perror("stat");
        return -1;
    }
    //获取文件类型和权限
    char perms[11] = {0}; //用于保存文件类型和文件权限的字符串
    switch(st.st_mode & __S_IFMT){
        case __S_IFLNK:
            perms[0] = 'l';
            break;
        case __S_IFDIR:
            perms[0] = 'd';
            break;
        case __S_IFREG:
            perms[0] = '-';
            break;
        case __S_IFBLK:
            perms[0] = 'b';
            break;
        case __S_IFCHR:
            perms[0] = 'c';
            break;
        case __S_IFSOCK:
            perms[0] = 's';
            break;
        case __S_IFIFO:
            perms[0] = 'p';
            break;
        default:
            perms[0] = '?';
    }

    //文件权限
    //文件所有者
    perms[1] = st.st_mode & S_IRUSR ? 'r' :'-';
    perms[2] = st.st_mode & S_IWUSR ? 'w' :'-';
    perms[3] = st.st_mode & S_IXUSR ? 'x' :'-';
    //文件所在组
    perms[4] = st.st_mode & S_IRGRP ? 'r' :'-';
    perms[5] = st.st_mode & S_IWGRP ? 'w' :'-';
    perms[6] = st.st_mode & S_IXGRP ? 'x' :'-';
    //其他人
    perms[7] = st.st_mode & S_IROTH ? 'r' :'-';
    perms[8] = st.st_mode & S_IWOTH ? 'w' :'-';
    perms[9] = st.st_mode & S_IXOTH ? 'x' :'-';

    //硬连接数
    int linkNUm = st.st_nlink;

    //文件所有者
    char* fileuser = getpwuid(st.st_uid)->pw_name;

    //文件所在组
    char* filegroup = getgrgid(st.st_gid)->gr_name;

    //文件大小
    long int filesize = st.st_size;
    //修改的时间
    char* time = ctime(&st.st_mtime);

    char mtime[512] = {0};
    strncpy(mtime, time, strlen(time) - 1);

    char buf[1024];
    sprintf(buf, "%s %d %s %s %ld %s %s", perms, linkNUm,fileuser, filegroup, filesize, mtime, argv[1]);

    printf("%s\n", buf);

    return 0;
}
相关推荐
Johny_Zhao9 小时前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
YuMiao1 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
chlk1232 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑2 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件2 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒2 天前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI3 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行4 天前
Linux和window共享文件夹
linux