系统编程之高级文件IO(十一)——获取设置文件属性(fcntl、ioctl)

文章目录

一、fcntl

  • 通过fcntl可以设置、或者修改已打开的文件性质
  • int fcntl(int fd, int cmd, …/* arg */);
  • fd,指向打开文件
  • cmd,控制命令,通过指定不同的宏来修改fd所指向文件的性质
  • 调用成功:返回值视具体参数而定;调用失败:返回-1,并把错误信号设置给errno
    在这里插入图片描述
#include <stdio.h>
#include <stdlib.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
    
    
    int fd;

    if ((fd = open(argv[1], O_RDWR | O_CREAT, 0655)) < 0)
    {
    
    
        perror("open file error!");
        exit(1);
    }

    int flags;

    flags = fcntl(fd, F_GETFL);

    flags = flags | O_APPEND;

    fcntl(fd, F_SETFL, flags);
    
    if(write(fd, "hello", 5) < 0)
    {
    
    
        perror("write data error!");
        exit(1);
    }

    close(fd);

    return 0;
}

不能修改读写的标志位
先获取标志位,再修改

二、ioctl

在这里插入图片描述
cmd是用户定义的
驱动程序独有的ioctl

猜你喜欢

转载自blog.csdn.net/m0_52592798/article/details/124296375