|Skip Navigation Links|
|Exit Print View|
|man pages section 3: Basic Library Functions Oracle Solaris 10 8/11 Information Library|
- open a stream
/usr/ucb/cc [ flag ... ] file ... #include <stdio.h> FILE *fopen(file, mode) const char *file, *mode;
FILE *freopen(file, mode, iop) const char *file, *mode; register FILE *iop;
The fopen() function opens the file specified by file and associates a stream with it. If the open succeeds, fopen() returns a pointer to be used to identify the stream in subsequent operations. The file argument points to a character string that contains the name of the file to be opened. The mode argument is a character string having one of the following values:
open for reading
truncate or create for writing
append: open for writing at end of file, or create for writing
open for update (reading and writing)
truncate or create for update
append; open or create for update at EOF
The freopen() function opens the file specified by file and associates the stream pointed to by iop with it. The mode argument is used just as in fopen(). The original stream is closed, regardless of whether the open ultimately succeeds. If the open succeeds, freopen() returns the original value of iop.
The freopen() function is typically used to attach the preopened streams associated withstdin, stdout, and stderr to other files.
When a file is opened for update, both input and output can be performed on the resulting stream. Output cannot be directly followed by input without an intervening fseek(3C) or rewind(3C). Input cannot be directly followed by output without an intervening fseek(3C) or rewind(3C). An input operation that encounters EOF will fail.
The fopen() and freopen() functions return a NULL pointer on failure.
The fopen() and freopen() functions have transitional interfaces for 64-bit file offsets. See lf64(5).
Use of these functions should be restricted to applications written on BSD platforms. Use of these functions with any of the system libraries or in multithreaded applications is unsupported.
To support the same number of open files as the system, fopen() must allocate additional memory for data structures using malloc(3C) after 64 files have been opened. This confuses some programs that use their own memory allocators.
The fopen() and freopen() functions differ from the standard I/O functions fopen(3C) and freopen(3C). The standard I/O functions distinguish binary from text files with an additional use of 'b' as part of the mode, enabling portability of fopen(3C) and freopen(3C) beyond SunOS 4.x systems.