Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > F

fopen(3S)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

NAME

fopen(), freopen(), fdopen() — open/reopen a stream file or convert file to stream

SYNOPSIS

#include <stdio.h>

FILE *fopen(const char *__restrict pathname, const char *__restrict type);

FILE *freopen(const char *__restrict pathname, const char *__restrict type, FILE *__restrict stream);

FILE *fdopen(int fildes, const char *type);

DESCRIPTION

fopen()

Opens the file named by pathname and associates a stream with it. fopen() returns a pointer to the FILE structure associated with the stream.

freopen()

substitutes the named file in place of the open stream. The original stream is closed, regardless of whether the open ultimately succeeds. freopen() returns a pointer to the FILE structure associated with stream and makes an implicit call to clearerr() (see ferror(3S)). After a successful call to freopen(), the orientation of the stream is cleared (see orientation(5)).

freopen() is typically used to attach the preopened streams associated with stdin, stdout, and stderr to other files.

fdopen()

associates a stream with a file descriptor. File descriptors are obtained from open(), dup(), creat(), or pipe() (see open(2), dup(2), creat(2), and pipe(2)), which open files but do not return pointers to a FILE structure stream. Streams are necessary input for many of the Section (3S) library routines. The type of stream must agree with the mode of the open file. The meanings of type used in the fdopen() call are exactly as specified above, except that w, w+, wb, and wb+ do not cause truncation of the file.

pathname

Points to a character string containing the name of the file to be opened.

type

Character string having one of the values listed below. The b in the following values has no effect. It exists to distinguish binary files from text files; however, there is no distinction between these types of files on UNIX systems (it is required for ISO C standard conformance).

r or rb

open file for reading

w or wb

truncate to zero length or create file for writing

a or ab

append; open file for writing at end of file, or create file or writing

r+, rb+, or r+b

open file for update (reading and writing)

w+, wb+, or w+b

truncate file to zero length or create file for update

a+, ab+, or a+b

append; open or create file for update at end-of-file

When a file is opened for update, both input and output can be done on the resulting stream. However, output cannot be directly followed by input without an intervening call to fflush() or to a file positioning function (fseek(), fsetpos(), or rewind()), and input cannot be directly followed by output without an intervening call to a file positioning function unless the input operation encounters end-of-file.

When a file is opened for append (that is, when type is a, a+, ab+, or a+b), it is impossible to overwrite information already in the file. All output is written at the end of the file, regardless of intervening calls to fseek(). If two separate processes open the same file for append, each process can write freely to the file without fear of destroying output being written by the other. Output from the two processes will be intermixed in the file in the order in which it is written.

Notes

HP-UX binary file types are equivalent to their non-binary counterparts. For example, types r and rb are equivalent.

RETURN VALUE

Upon successful completion, fopen(), fdopen() and freopen() return a FILE * pointer to the stream. Otherwise, a null pointer is returned and errno is set to indicate the error.

ERRORS

fopen(), fdopen(), and freopen() fail if:

EBADF

The fildes argument is not a valid file descriptor.

EINVAL

The type argument is not a valid mode.

ENOMEM

There is insufficient space to allocate a buffer.

fopen() and freopen() fail if:

EACCES

Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by type are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.

EINTR

A signal was caught during fopen() or freopen(). function.

EISDIR

The named file is a directory and type requires write access.

EMFILE

The calling process has attempted to exceed its open file limit.

ENAMETOOLONG

The length of the pathname string exceeds PATH_MAX or a pathname component is longer than NAME_MAX while POSIX_NO_TRUNC is in effect.

ENFILE

The system file table is full.

ENOENT

The named file does not exist or the pathname argument points to an empty string.

ENOSPC

The directory or file system that would contain the new file cannot be expanded, the file does not exist, and it was to be created.

ENOTDIR

A component of the path prefix is not a directory.

ENXIO

The named file is a character special or block special file, and the device associated with the special file does not exist.

EOVERFLOW

The named file is a regular file and the size of the file cannot be represented correctly in an object of size off_t in this environment.

EROFS

The named file resides on a read-only file system and type requires write access.

Additional errno values can be set by the underlying open() call made from the fopen() and freopen() functions (see open(2)).

STANDARDS CONFORMANCE

fopen(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C

fdopen(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1

freopen(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.