#include<fcnt1.h>
#include<sys/types.h>
#include<sys/stat.h>
int open( const char *path , int oflags )
int open(const char *path , int oflags , mode_t mode )
open 建立一条到文件或设备的访问路径。
准备打开的文件或设备的名字作为参数path传递给函数,oflags参数用于指定打开文件所采取的动作。
open调用必须指定下列三种模式中的一种:
O_RDONLY 以只读方式打开
O_WRONLY 以只写方式打开
O_RDWR 以读写方式打开
open调用还可以在oflags参数中包括下列可选模式的组合(用 “按位或”操作)
O_APPEND:把写入的数据追加在文件的末尾
O_TRUNC:把文件长度设置为零,丢弃已有的内容
O_CREAT:如果需要,按照参数mode中给出的访问模式创建文件
O_EXCL:与O_CREAT一起,确保调用者创建出文件。使用这种模式,可以防止两个程序同时创建同一个文件,如果文件已经存在,open调用将失败。
mode:当你使用带有O_CREAT标志的open调用来创建文件时,你必须使用3个参数格式的open调用。第三个参数mode是几个标志位按位或得到的,这些
标志位在头文件sys/stat.h中定义,
S_IRUSR: 读权限,文件属主
S_IWUSR: 写权限,文件属主
S_IXUSR: 执行权限,文件属主
S_IRGRP: 读权限,文件属组
S_IWGRP:
S_IXGRP:
S_IROTH:
S_IWORH:
S_IXOTH:
#include <unistd.h>
int close(int fildes);
#include<unistd.h>
int ioct1(int fildes,int cmd,...);
ioct1调用有点像是大杂烩。它提供了一个用于控制设备及其描述符行为和配置底层服务的接口。
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
char c;
int in,out;
in = open("file.in",O_RDONLY);
out =open("file.out",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
while(read(in,&c,1) == 1)
write(out,&c,1);
}
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
char block[1024];
int in,out;
int nread;
in = open("file.in",O_RDONLY);
out = open("file.out",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
while((nread = read(in,block, sizeof(block))) > 0)
write(out,block,nread);
exit(0);
}