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;
}
相关推荐
江华森3 小时前
Spring Cloud 微服务全栈实战:从 Eureka 到 Docker Compose 一文贯通
运维
江华森3 小时前
Matplotlib 数据绘图基础入门
运维
XIAOHEZIcode3 小时前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
江华森3 小时前
NumPy 数值计算基础入门
运维
唐青枫5 小时前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux
AlfredZhao2 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
戴为沐3 天前
Linux内存扩容指南
linux
zylyehuo4 天前
Linux 彻底且安全地删除文件
linux
用户805533698034 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297914 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux