OPEN函数

open 函数用于打开和创建文件。以下是 open 函数的简单描述

#include <fcntl.h>

int open(const char *pathname, int oflag, ... /* mode_t mode */);

返回值:成功则返回文件描述符,否则返回 -1

对于 open 函数来说,第三个参数(...)仅当创建新文件时才使用,用于指定文件的访问权限位(access permission bits)。

pathname 是待打开/创建文件的路径名(如/home/hello.c);

oflag 用于指定文件的打开/创建模式,这个参数可由以下常量(定义于 fcntl.h)通过逻辑或构成。

O_RDONLY 只读模式

O_WRONLY 只写模式

O_RDWR 读写模式

打开/创建文件时,至少得使用上述三个常量中的一个。

例如,打开一个文件:

#include<stdio.h>

#include<fcntl.h>
void main(){
int fd;
fd=open("/home/File_Pro/File_IO/lseek1.c",2);
if(fd==-1)
printf("open fail ");

else printf("open success fd=%d ",fd);
}

原文地址:https://www.cnblogs.com/hezhangyear/p/4020192.html