⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fcntl_access.c

📁 LINUX C编程实战这本书附带光盘的原代码
💻 C
字号:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

/*自定义的错误处理函数*/
void my_err(const char * err_string, int line)
{
	fprintf(stderr, "line:%d  ", line);
	perror(err_string);
	exit(1);
}

int main()
{
	int	ret;
	int	access_mode;
	int	fd;

	if ((fd = open("example_64", O_CREAT|O_TRUNC|O_RDWR, S_IRWXU)) == -1) {
		my_err("open", __LINE__);
	}

	/*设置文件打开方式*/
	if ((ret = fcntl(fd, F_SETFL, O_APPEND)) < 0) {
		my_err("fcntl", __LINE__);
	}

	/*获取文件打开方式*/
	if ((ret = fcntl(fd, F_GETFL, 0)) < 0) {
		my_err("fcntl", __LINE__);
	}
	access_mode = ret & O_ACCMODE;
	if (access_mode == O_RDONLY) {
		printf("example_64 access mode: read only");
	} else if (access_mode == O_WRONLY) {
		printf("example_64 access mode: write only");
	} else if (access_mode == O_RDWR) {
		printf("example_64 access mode: read + write");
	}

	if (ret & O_APPEND) {
		printf(" ,append");
	}
	if (ret & O_NONBLOCK) {
		printf(" ,nonblock");
	}
	if (ret & O_SYNC) {
		printf(", sync");
	}
	printf("\n");

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -