Print this page
3687 fopen() O_CLOEXEC support via the "e" flag
   1 '\" te
   2 .\" Copyright 1989 AT&T.
   3 .\" Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved.
   4 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.

   5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
   6 .\" http://www.opengroup.org/bookstore/.
   7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
   8 .\"  This notice shall appear on any product containing this material.
   9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
  10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
  11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
  12 .TH FOPEN 3C "Apr 18, 2006"
  13 .SH NAME
  14 fopen \- open a stream
  15 .SH SYNOPSIS
  16 .LP
  17 .nf
  18 #include <stdio.h>
  19 
  20 \fBFILE *\fR\fBfopen\fR(\fBconst char *\fR\fIfilename\fR, \fBconst char *\fR\fImode\fR);
  21 .fi
  22 
  23 .SH DESCRIPTION
  24 .sp
  25 .LP
  26 The \fBfopen()\fR function opens the file whose pathname is the string pointed
  27 to by \fIfilename\fR, and associates a stream with it.
  28 .sp
  29 .LP
  30 The argument \fImode\fR points to a string beginning with one of the following
  31 sequences:
  32 .sp
  33 .ne 2
  34 .na
  35 \fB\fBr\fR or \fBrb\fR\fR
  36 .ad
  37 .RS 20n
  38 Open file for reading.
  39 .RE
  40 
  41 .sp
  42 .ne 2
  43 .na
  44 \fB\fBw\fR or \fBwb\fR\fR
  45 .ad
  46 .RS 20n
  47 Truncate to  zero  length or create file for writing.
  48 .RE
  49 
  50 .sp
  51 .ne 2
  52 .na
  53 \fB\fBa\fR or \fBab\fR\fR
  54 .ad
  55 .RS 20n
  56 Append; open or create file for writing at end-of-file.
  57 .RE
  58 
  59 .sp
  60 .ne 2
  61 .na
  62 \fB\fBr+\fR or \fBrb+\fR or \fBr+b\fR\fR
  63 .ad
  64 .RS 20n
  65 Open file for update (reading and writing).
  66 .RE
  67 
  68 .sp
  69 .ne 2
  70 .na
  71 \fB\fBw+\fR or \fBwb+\fR or \fBw+b\fR\fR
  72 .ad
  73 .RS 20n
  74 Truncate to zero length or create file for update.
  75 .RE
  76 
  77 .sp
  78 .ne 2
  79 .na
  80 \fB\fBa+\fR or \fBab+\fR or \fBa+b\fR\fR
  81 .ad
  82 .RS 20n
  83 Append; open or create file for update, writing at end-of-file.
  84 .RE
  85 
  86 .sp
  87 .LP






  88 The character \fBb\fR has no effect, but is allowed for ISO C standard
  89 conformance (see \fBstandards\fR(5)). Opening a file with read mode (\fBr\fR as
  90 the first character in the \fImode\fR argument) fails if the file does not
  91 exist or cannot be read.
  92 .sp
  93 .LP




  94 Opening a file with append mode (\fBa\fR as the first character in the
  95 \fImode\fR argument) causes all subsequent writes to the file to be forced to
  96 the then current end-of-file, regardless of intervening calls to
  97 \fBfseek\fR(3C). If two separate processes open the same file for append, each
  98 process may write freely to the file without fear of destroying output being
  99 written by the other.  The output from the two processes will be intermixed in
 100 the file in the order in which it is written.
 101 .sp
 102 .LP
 103 When a file is opened with update mode (\fB+\fR as the second or third
 104 character in the \fImode\fR argument), both input and output may be performed
 105 on the associated stream. However, output must not be directly followed by
 106 input without an intervening call to \fBfflush\fR(3C) or to a file positioning
 107 function ( \fBfseek\fR(3C), \fBfsetpos\fR(3C) or \fBrewind\fR(3C)), and input
 108 must not be directly followed by output without an intervening call to a file
 109 positioning function, unless the input operation encounters end-of-file.
 110 .sp
 111 .LP
 112 When opened, a stream is fully buffered if and only if it can be determined not
 113 to refer to an interactive device. The error and end-of-file indicators for the


 375 .TS
 376 box;
 377 c | c
 378 l | l .
 379 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 380 _
 381 Interface Stability     See below.
 382 _
 383 MT-Level        MT-Safe
 384 .TE
 385 
 386 .sp
 387 .LP
 388 The \fBF\fR character in the \fImode\fR argument is Evolving. In all other
 389 respects this function is Standard.
 390 .SH SEE ALSO
 391 .sp
 392 .LP
 393 \fBenable_extended_FILE_stdio\fR(3C), \fBfclose\fR(3C), \fBfdopen\fR(3C),
 394 \fBfflush\fR(3C), \fBfreopen\fR(3C), \fBfsetpos\fR(3C), \fBrewind\fR(3C),
 395 \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5)
   1 '\" te
   2 .\" Copyright 1989 AT&T.
   3 .\" Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved.
   4 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
   5 .\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc.  All Rights Reserved.
   6 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
   7 .\" http://www.opengroup.org/bookstore/.
   8 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
   9 .\"  This notice shall appear on any product containing this material.
  10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
  11 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
  12 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
  13 .TH FOPEN 3C "Apr 18, 2006"
  14 .SH NAME
  15 fopen \- open a stream
  16 .SH SYNOPSIS
  17 .LP
  18 .nf
  19 #include <stdio.h>
  20 
  21 \fBFILE *\fR\fBfopen\fR(\fBconst char *\fR\fIfilename\fR, \fBconst char *\fR\fImode\fR);
  22 .fi
  23 
  24 .SH DESCRIPTION
  25 .sp
  26 .LP
  27 The \fBfopen()\fR function opens the file whose pathname is the string pointed
  28 to by \fIfilename\fR, and associates a stream with it.
  29 .sp
  30 .LP
  31 The argument \fImode\fR points to a string beginning with one of the following
  32 base sequences:
  33 .sp
  34 .ne 2
  35 .na
  36 \fB\fBr\fR\fR 
  37 .ad
  38 .RS 20n
  39 Open file for reading.
  40 .RE
  41 
  42 .sp
  43 .ne 2
  44 .na
  45 \fB\fBw\fR\fR
  46 .ad
  47 .RS 20n
  48 Truncate to  zero  length or create file for writing.
  49 .RE
  50 
  51 .sp
  52 .ne 2
  53 .na
  54 \fB\fBa\fR\fR
  55 .ad
  56 .RS 20n
  57 Append; open or create file for writing at end-of-file.
  58 .RE
  59 
  60 .sp
  61 .ne 2
  62 .na
  63 \fB\fBr+\fR\fR
  64 .ad
  65 .RS 20n
  66 Open file for update (reading and writing).
  67 .RE
  68 
  69 .sp
  70 .ne 2
  71 .na
  72 \fB\fBw+\fR\fR
  73 .ad
  74 .RS 20n
  75 Truncate to zero length or create file for update.
  76 .RE
  77 
  78 .sp
  79 .ne 2
  80 .na
  81 \fB\fBa+\fR\fR
  82 .ad
  83 .RS 20n
  84 Append; open or create file for update, writing at end-of-file.
  85 .RE
  86 
  87 .sp
  88 .LP
  89 In addition to the base sequences for the \fImode\fR argument above, two
  90 additional flags are supported via the \fBb\fR character and the \fBe\fR
  91 character.  Order of these additional flags (including the \fB+\fR) does
  92 not matter.
  93 .sp
  94 .LP
  95 The character \fBb\fR has no effect, but is allowed for ISO C standard
  96 conformance (see \fBstandards\fR(5)). Opening a file with read mode (\fBr\fR as
  97 the first character in the \fImode\fR argument) fails if the file does not
  98 exist or cannot be read.
  99 .sp
 100 .LP
 101 The character \fBe\fR will cause the underlying file descriptor to be
 102 opened with the O_CLOEXEC flag as described in \fBopen\fR(2).
 103 .sp
 104 .LP
 105 Opening a file with append mode (\fBa\fR as the first character in the
 106 \fImode\fR argument) causes all subsequent writes to the file to be forced to
 107 the then current end-of-file, regardless of intervening calls to
 108 \fBfseek\fR(3C). If two separate processes open the same file for append, each
 109 process may write freely to the file without fear of destroying output being
 110 written by the other.  The output from the two processes will be intermixed in
 111 the file in the order in which it is written.
 112 .sp
 113 .LP
 114 When a file is opened with update mode (\fB+\fR as the second or third
 115 character in the \fImode\fR argument), both input and output may be performed
 116 on the associated stream. However, output must not be directly followed by
 117 input without an intervening call to \fBfflush\fR(3C) or to a file positioning
 118 function ( \fBfseek\fR(3C), \fBfsetpos\fR(3C) or \fBrewind\fR(3C)), and input
 119 must not be directly followed by output without an intervening call to a file
 120 positioning function, unless the input operation encounters end-of-file.
 121 .sp
 122 .LP
 123 When opened, a stream is fully buffered if and only if it can be determined not
 124 to refer to an interactive device. The error and end-of-file indicators for the


 386 .TS
 387 box;
 388 c | c
 389 l | l .
 390 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 391 _
 392 Interface Stability     See below.
 393 _
 394 MT-Level        MT-Safe
 395 .TE
 396 
 397 .sp
 398 .LP
 399 The \fBF\fR character in the \fImode\fR argument is Evolving. In all other
 400 respects this function is Standard.
 401 .SH SEE ALSO
 402 .sp
 403 .LP
 404 \fBenable_extended_FILE_stdio\fR(3C), \fBfclose\fR(3C), \fBfdopen\fR(3C),
 405 \fBfflush\fR(3C), \fBfreopen\fR(3C), \fBfsetpos\fR(3C), \fBrewind\fR(3C),
 406 \fBopen\fR(2), \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5)