相同点:
都是用来完成文件打开操作的函数
不同点:
open是系统调用,返回的是文件句柄,fopen是库函数,返回的是一个指向文件结构的指针
open与 read, write等配合使用,fopen与 fread, fwrite等配合使用
前者属于低级IO,后者是高级IO
open是内核级的,fopen是非内核级的
open无缓冲,fopen有缓冲
fopen是在open的基础上扩充而来的
open
---------------------------------------------------------
SYNOPSIS
#include
#include
#include
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
DESCRIPTION
The open() system call is used to convert a pathname into a file
descriptor (a small, non-negative integer for use in subsequent I/O as
with read, write, etc.). When the call is successful, the file
descriptor returned will be the lowest file descriptor not currently
open for the process. This call creates a new open file, not shared
with any other process. (But shared open files may arise via the
fork(2) system call.) The new file descriptor is set to remain open
across exec functions (see fcntl(2)). The file offset is set to the
beginning of the file.
The parameter flags is one of O_RDONLY, O_WRONLY or O_RDWR which
request opening the file read-only, write-only or read/write, respec-
tively, bitwise-or'd with zero or more of the following
O_CREAT
If the file does not exist it will be created. The owner (user
ID) of the file is set to the effective user ID of the process.
The group ownership (group ID) is set either to the effective
group ID of the process or to the group ID of the parent direc-
tory (depending on filesystem type and mount options, and the
mode of the parent directory, see, e.g., the mount options bsd-
groups and sysvgroups of the ext2 filesystem, as described in
mount(8)).
O_EXCL
When used with O_CREAT, if the file already exists it is an
error and the open will fail. In this context, a symbolic link
exists, regardless of where its points to. O_EXCL is broken on
NFS file systems, programs which rely on it for performing lock-
ing tasks will contain a race condition. The solution for per-
forming atomic file locking using a lockfile is to create a
unique file on the same fs (e.g., incorporating hostname and
pid), use link(2) to make a link to the lockfile. If link()
returns 0, the lock is successful. Otherwise, use stat(2) on
the unique file to check if its link count has increased to 2,
in which case the lock is also successful.
......
fopen
---------------------------------------------------------
SYNOPSIS
#include
FILE *fopen(const char *path, const char *mode);
DESCRIPTION
The fopen function opens the file whose name is the string pointed to
by path and associates a stream with it.
The argument mode points to a string beginning with one of the follow-
ing sequences (Additional characters may follow these sequences.):
r Open text file for reading. The stream is positioned at the
beginning of the file.
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
w Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is positioned
at the beginning of the file.
a Open for appending (writing at end of file). The file is cre-
ated if it does not exist. The stream is positioned at the end
of the file.
a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The stream is positioned
at the end of the file.
......
阅读(2223) | 评论(0) | 转发(0) |