免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1185 | 回复: 0
打印 上一主题 下一主题

fopen与open的区别 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-24 08:42 |只看该作者 |倒序浏览

                                相同点:
都是用来完成文件打开操作的函数
不同点:
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.
       ......   
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/43678/showart_345452.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP