1FOPEN(3S)                                                            FOPEN(3S)
2
3
4

NAME

6       fopen, freopen, fdopen - open a stream
7

SYNOPSIS

9       #include <stdio.h>
10
11       FILE *fopen(filename, type)
12       char *filename, *type;
13
14       FILE *freopen(filename, type, stream)
15       char *filename, *type;
16       FILE *stream;
17
18       FILE *fdopen(fildes, type)
19       char *type;
20

DESCRIPTION

22       Fopen opens the file named by filename and associates a stream with it.
23       Fopen returns a pointer to be used to identify the stream in subsequent
24       operations.
25
26       Type is a character string having one of the following values:
27
28       "r"  open for reading
29
30       "w"  create for writing
31
32       "a"  append: open for writing at end of file, or create for writing
33
34       In addition, each type may be followed by a "+" to have the file opened
35       for reading and writing.  "r+" positions the stream at the beginning of
36       the  file,  "w+"  creates or truncates it, and "a+" positions it at the
37       end.  Both reads and writes may be used on read/write streams, with the
38       limitation  that  an  fseek,  rewind, or reading an end-of-file must be
39       used between a read and a write or vice-versa.
40
41       Freopen substitutes the named file in place of  the  open  stream.   It
42       returns the original value of stream.  The original stream is closed.
43
44       Freopen  is  typically  used  to  attach  the preopened constant names,
45       stdin, stdout, stderr, to specified files.
46
47       Fdopen associates a stream with a file descriptor obtained  from  open,
48       dup,  creat,  or  pipe(2).   The type of the stream must agree with the
49       mode of the open file.
50

SEE ALSO

52       open(2), fclose(3)
53

DIAGNOSTICS

55       Fopen and freopen  return  the  pointer  NULL  if  filename  cannot  be
56       accessed,  if  too  many  files are already open, or if other resources
57       needed cannot be allocated.
58

BUGS

60       Fdopen is not portable to systems other than UNIX.
61
62       The read/write types do not exist on all systems.  Those systems  with‐
63       out read/write modes will probably treat the type as if the "+" was not
64       present.  These are unreliable in any event.
65
66       In order to support the same number of open files as does  the  system,
67       fopen  must allocate additional memory for data structures using calloc
68       after 20 files have been opened.  This confuses some programs which use
69       their  own memory allocators.  An undocumented routine, f_prealloc, may
70       be called to force immediate allocation of all internal  memory  except
71       for buffers.
72
73
74
754th Berkeley Distribution        May 27, 1986                        FOPEN(3S)
Impressum