zoukankan      html  css  js  c++  java
  • 深入探究文件I/O fcntl()

    文件控制操作

    对一个打开的文件描述符执行一系列控制操作

    • #include <unistd.h>
      #include <fcntl.h>
      int fcntl(int fd, int cmd, ... /* arg */ );
    • 1.获取或修改其访问模式和状态标志   (flag
    • 2.要获取这些设置  需要将fcntl 的 cmd参数设置成F_GETFL
    • F_SETFL  设置flags  改变状态 或 模式
    • 使用场景 文件不是由调用程序打开的  所以无法使用open() 系统调用来控制文件的状态标志。
    • 文件描述符的获取时通过open ()以外的函数系统调用 如pipe()  socket()等。
    •  myfcntl.c+ � �                                                                                                                                              �� buffers
          1 #include <stdio.h>
          2 #include<stdlib.h>
          3 #include<string.h>
          4 #include<sys/types.h>
          5 #include<sys/stat.h>
          6 #include<fcntl.h>
          7 #include<unistd.h>
          89 int main(int argc,char* argv[])
         10 {
         11     int flags;
         12     int accessMode;
         13     int fd = open(argv[1],O_RDWR);
         14     flags = fcntl(fd,F_GETFL);
         15     if(flags==-1)
         16     {
         17         perror("get error");
         18         exit(-1);
         19     }
         20     accessMode = flags& O_ACCMODE;
         21     if(accessMode==O_RDWR||accessMode==O_WRONLY)
         22     {
         23         printf("file is writable;");
         24     }
         25     flags|=O_APPEND;
         26     flags = fcntl(fd,F_SETFL,flags);
         27     printf("flags: %d",flags);
         28     return 0;
         29 }
         30
        ~
  • 相关阅读:
    csp-s模拟110
    csp-s模拟109
    留念
    csp-s 2019 游记
    HEOI2020
    CSP-S2019记
    堆积的$TIPS$
    低错复习
    倍增并查集
    4.26
  • 原文地址:https://www.cnblogs.com/jingchu/p/10274507.html
Copyright © 2011-2022 走看看