fopen(3C)
_________________________________________________________________
fopen function
Open a file.
_________________________________________________________________
Calling Sequence
#include <stdio.h>
FILE *fopen(), *stream;
char *filename, *type;
stream = fopen(filename, type);
where
filename is a byte pointer to the pathname of the file.
type is a byte pointer to a string specifying one of the
standard opening types (all are UNIX compatible):
a Open for appending only.
a+ Open for appending (read and write).
r Open for reading only.
r+ Open for read/write access. Return an error if the file
does not exist.
w Delete, recreate, and open for writing only.
w+ Delete, recreate, and open a file for read/write access.
Description
Use the fopen function to open a file. As long as your file is a
disk file, you need not take any special action to alternate
between reading and writing; the I/O routines correctly switch
from one mode to the other. If your program will run under other
C compilers, or is not a disk file, you might have to use an
fseek(stream, 0L, 1) when switching I/O types.
If the file does not exist, the function tries to create it
unless the mode is r. With the w option, the function always
deletes the existing file first and then re-creates it.
If you open a file under any option except s and it is updated by
more than one process, there can be no guarantee of the file's
contents.
If you use the s option, you must reserve one or more pages with
the /RESERVE= switch on the CCL link macro.
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
fopen(3C)
Returns
The function returns a null if the opening fails. Otherwise, it
returns a pointer to pass to the other I/O routines.
Related Functions
See also the fclose, fdopen, and freopen functions.
Example
/* Program test for the fopen() function */
#include <stdio.h>
#define CHARMAX 80
#define STRINGMAX 5
FILE *fpi, *fpo, *fopen();
char stringbuf[CHARMAX];
int i = 0;
main(argc, argv)
int argc;
char *argv[];
{
fpi = fopen(argv[1], "r");
fpo = fopen(argv[2], "a");
while (i < STRINGMAX) {
fgets(stringbuf, CHARMAX, fpi);
printf("%s", stringbuf);
fputs(stringbuf, fpo);
i++;
}
}
A call to the program test with the valid filenames infile and
outfile, where the file infile contains the following:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
and the file outfile contains
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)
fopen(3C)
You did not append this line or the next.
The following lines are from infile:
generates the terminal output
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
and the file outfile will now contain the following:
You did not append this line or the next.
The following lines are from infile:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
DG/UX 4.00 Page 3
Licensed material--property of copyright holder(s)