Linux系统编程——fcntl函数

90 篇文章 9 订阅

fcntl函数

  • 函数描述: 改变已经打开的文件的属性

  • 函数原型: int fcntl(int fd, int cmd, … /* arg */ );

    若cmd为F_DUPFD, 复制文件描述符, 与dup相同

    若cmd为F_GETFL, 获取文件描述符的flag属性值

    若cmd为 F_SETFL, 设置文件描述符的flag属性

  • 函数返回值:返回值取决于cmd

    • 成功

    若cmd为F_DUPFD, 返回一个新的文件描述符
    若cmd为F_GETFL, 返回文件描述符的flags值
    若cmd为 F_SETFL, 返回0

    • 失败

    返回-1, 并设置errno值.

fcntl函数常用的操作:

1 复制一个新的文件描述符:

int newfd = fcntl(fd, F_DUPFD, 0);

2 获取文件的属性标志

int flag = fcntl(fd, F_GETFL, 0)

3 设置文件状态标志

flag = flag | O_APPEND;

fcntl(fd, F_SETFL, flag)

4 常用的属性标志

O_APPEND-----设置文件打开为末尾添加

O_NONBLOCK-----设置打开的文件描述符为非阻塞

练习

  1. 使用fcntl函数实现复制文件描述符

  2. 使用fcntl函数设置在打开的文件末尾添加内容.

1. 使用fcntl函数实现复制文件描述符

//测试fcntl函数复制文件描述符
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	//打开文件
	int fd = open(argv[1], O_RDWR);
	if(fd<0)
	{
		perror("open error");
		return -1;
	}

	//调用fcntl函数复制fd,注意,用dup也可以实现一样的功能
	//int newfd = dup(fd);
	int newfd = fcntl(fd,F_DUPFD,0);
	printf("newfd:[%d], fd:[%d]\n", newfd, fd);

	//使用fd对文件进行写操作
	write(fd, "hello world", strlen("hello world"));

	//调用lseek函数移动文件指针到开始处
	lseek(fd, 0, SEEK_SET);

	//使用newfd读文件
	char buf[64];
	memset(buf, 0x00, sizeof(buf));
	int n = read(newfd, buf, sizeof(buf));
	printf("read over: n==[%d], buf==[%s]\n", n, buf);

	//关闭文件
	close(fd);
	close(newfd);

	return 0;
}

2. 使用fcntl函数设置在打开的文件末尾添加内容

修改文件描述符的flag属性,添加O_APPEND标记,实现在文件末尾添加内容。

//修改文件描述符的flag属性
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	//打开文件
	int fd = open(argv[1], O_RDWR);
	if(fd<0)
	{
		perror("open error");
		return -1;
	}

	//获得和设置fd的flags属性
	int flags = fcntl(fd, F_GETFL, 0);
	flags = flags | O_APPEND;
	fcntl(fd, F_SETFL, flags);

	//写文件
	write(fd, "hello world", strlen("hello world"));

	//关闭文件
	close(fd);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值