fcntl获取和修改文件打开状态标志
[root@bogon code]# cat b.c
#include<stdio.h>
#include<error.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd=open("a.c",O_RDONLY);//以可读方式打开
int flags;
flags=fcntl(fd,F_GETFL);//用flags记录文件打开状态标志,flags的值至于open里面的打开方式有关,与打开的文件本身属性无关,也就是说假设a.c的属性为777,但是在open时是只以可读方式打开的,那么flags只能检测出可读
if(flags==-1)
perror("fcntl");
if(flags&O_RDWR)//检测是否可读可写
printf("can read can write\n");
else
printf("just can read\n");
return 0;
}
[root@bogon code]# gcc b.c
[root@bogon code]# ./a.out
just can read
[root@bogon code]#
上面这个程序虽然没有错,不过更正确的写法应该是下面这个
[root@bogon code]# cat b.c
#include<stdio.h>
#include<error.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd=open("a.c",O_RDWR);
int flags,accessMode;
flags=fcntl(fd,F_GETFL);
if(flags==-1)
perror("fcntl");
accessMode=flags&O_ACCMODE;
if(accessMode==O_RDWR)
printf("can read can write\n");
else
printf("just can read\n");
return 0;
}
[root@bogon code]# gcc b.c
[root@bogon code]# ./a.out
can read can write
[root@bogon code]#
接下来我们来见识一下fcntl是如何修改文件打开状态标志的
哪些情况下我们需要修改文件状态标志呢
一:文件不是由调用程序打开,所以程序也无法使用open函数来控制文件的状态标志,例如标准输入输出描述符
二:文件描述符的获取是通过open之外的系统调用,例如pipe以及socket等。
[root@bogon code]# cat b.c
#include<stdio.h>
#include<error.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd=open("a.c",O_RDWR);
int flags,accessMode;
flags=fcntl(fd,F_SETFL);
if(flags==-1)
perror("fcntl");
flags|=O_APPEND;//添加O_APPEND标志
fcntl(fd,F_SETFL,flags);//设置O_APPEND标志
if(flags==O_APPEND)
printf("can append\n");
if(flags==O_RDWR)//我这里只是用来测试原来的状态标志会不会改变,从结果来看,貌似会的
printf("just can read and write\n");
else
printf("just can't read and write\n");
return 0;
}
[root@bogon code]# gcc b.c
[root@bogon code]# ./a.out
can append
just can't read and write
[root@bogon code]#
fcntl获取和修改文件打开状态标志的更多相关文章
- 第七篇:使用 fcntl 函数 获取,设置文件的状态标志
前言 当打开一个文件的时候,我们需要指定打开文件的模式( 只读,只写等 ).那么在程序中如何获取,修改这个文件的状态标志呢? 本文将告诉你如何用 fcntl函数 获取指定文件的状态标志. 解决思路 1 ...
- 使用 fcntl 函数 获取,设置文件的状态标志
前言 当打开一个文件的时候,我们需要指定打开文件的模式( 只读,只写等 ).那么在程序中如何获取,修改这个文件的状态标志呢?本文将告诉你如何用 fcntl函数 获取指定文件的状态标志. 解决思路 1. ...
- fcntl()函数之文件打开状态
比较有用的就两个. 1.获得/设置文件打开状态标志 oldflag=fcntl(fd,F_GETFL); 获得打开文件的状态标志. arg=oldflag|O_APPEND; fcntl(fd,F_S ...
- C#获取并修改文件扩展名的方法
本文实例讲述了C#获取并修改文件扩展名的方法.分享给大家供大家参考.具体分析如下: 这里使用C#编程的方法改变文件扩展名的文件,必须使用Path类. Path类用来解析文件系统路径的各个部分.静态方法 ...
- 在Delphi中获取和修改文件的时间
转载自 http://www.cnblogs.com/jieke/archive/2013/01/11/2855782.html 本文介绍了在Delphi中利用系统函数和Windows API函数调用 ...
- linux修改文件打开最大数(ulimit命令)
解除 Linux 系统的最大进程数和最大文件打开数限制:vi /etc/security/limits.conf# 添加如下的行* soft noproc 65536 * hard noproc 65 ...
- linux编程fcntl获取和设置文件状态
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> ...
- linux 修改文件打开数量限制
1.查看打开文件数量限制 ulimit -a ulimit -n 2.临时修改 ulimit -n 2048 3.永久修改 vi /etc/security/limits.conf 追加 * soft ...
- Centos修改文件打开数限制
一.查看系统限制最大打开数 cat /proc/sys/fs/file-max 还有一个问题是file-max最大能设置多大呢?一个经验算法是 256个fd 需4M内存.例如8G内存,8*1024/4 ...
随机推荐
- python操作Excel读写(使用xlrd和xlrt)
包下载地址:https://pypi.python.org/pypi/xlrd 导入 import xlrd 打开excel data = xlrd.open_workbook('demo.xls ...
- 打开和写入word文档
一. 使用win32读取word内容 # -*- coding: utf-8 -*- from win32com import client as wc def readDocx2(): word = ...
- Kubenates熟悉
Kuberetes命令,可用于查看信息和排查故障. kubectl cluster-info --context dev 查看master和服务的地址 kubectl config view 查看ku ...
- python的编码与转码
编码问题一直是初学者的难题,搞不明白.甚至一些程序员做了多年的程序,但是编码一直整不清,下面就来认识认识编码吧. ASCII(American Standard Code for Informatio ...
- codeforce150A(简单的求质数问题)
A. Win or Freeze time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- LeetCode子集问题
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(子集当中不包括重复的元素) 代码如下: def subsets(nums): target=[[]] for num in nums ...
- shell开源跳板机sshstack
笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...
- xargs用法
xargs是一个很有用的命令,它可以实现并行,同&有异曲同工之妙,在大批量管理服务器时非常有用 xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具.它擅长将标准输入数据 ...
- 基于区域的OSPF的MD5认证
实验要求:掌握OSPF基于区域的MD5认证 拓扑如下: 配置如下: R1enable configure terminal interface s0/0/0ip address 192.168.1.1 ...
- Spring 配置文件
<?xml version="1.0" encoding="UTF-8" ?> <beans> <bean id=...> ...