FOPEN(3s,L) AIX Technical Reference FOPEN(3s,L) ------------------------------------------------------------------------------- fopen, freopen, fdopen PURPOSE Opens a stream. LIBRARY Standard I/O Library (libc.a) SYNTAX #include FILE *fopen (path, type) FILE *fdopen (fildes, type) char *path, *type; int fildes; char *type; FILE *freopen (path, type, stream) char *path, *type; FILE *stream; DESCRIPTION The fopen subroutine opens the file named by the path parameter and associates a stream with it. fopen returns a pointer to the FILE structure of this stream. The path parameter points to a character string that contains the name of the file to be opened. The type parameter points to a character string that has one of the following values: "r" Open the file for reading "w" Truncate or create a new file for writing "a" Append (open for writing at end of file, or create for writing) "r+" Open for update (reading and writing) "w+" Truncate or create for update "a+" Append (open or create for update at end of file) The freopen subroutine substitutes the named file in place of the open stream. The original stream is closed whether or not the open succeeds. freopen returns a pointer to the FILE structure associated with stream. The freopen subroutine is typically used to attach the pre-opened streams associated with stdin, stdout, and stderr to other files. Processed July 12, 1991 FOPEN(3s,L) 1 FOPEN(3s,L) AIX Technical Reference FOPEN(3s,L) The fdopen subroutine associates a stream with a file descriptor obtained from an open, dup, creat, or pipe system call. These system calls open files but do not return pointers to FILE structures. Many of the standard I/O library subroutines require pointers to FILE structures. Note that the type of stream specified must agree with the mode of the open file. When you open a file for update, you can perform both input and output operations on the resulting stream. However, an output operation cannot be directly followed by an input operation without an intervening fseek or rewind. Also, an input operation cannot be directly followed by an output operation without an intervening fseek, rewind, or an input operation that encounters the end of the file. When you open a file for append (that is, when type is "a" or "a+"), it is impossible to overwrite information already in the file. You can use fseek to reposition the file pointer to any position in the file, but when output is written to the file, the current file pointer is ignored. All output is written at the end of the file and causes the file pointer to be repositioned to the end of the output. If two separate processes open the same file for append, each process can write freely to the file without destroying the output being written by the other. The output from the two processes is intermixed in the order in which it is written to the file. Note that if the data is buffered, then it is not actually written until it is flushed. If the fopen or freopen subroutine fails, a NULL pointer is returned. ERROR CONDITIONS The fopen and freopen subroutines fail if one or more of the following are true: EACCES Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by mode 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 the fopen function. EISDIR The named file is a directory and mode requires write access. EMFILE FOPEN_MAX file descriptors, directories and message catalogs are currently open in the calling process. ENAMETOOLONG The length of the filename string exceeds PATH_MAX or a pathname component is longer than NAME_MAX. ENFILE The system file table is full. Processed July 12, 1991 FOPEN(3s,L) 2 FOPEN(3s,L) AIX Technical Reference FOPEN(3s,L) ENOENT The named file does not exist or the filename argument points to an empty string. ENOSPC The directory or file system that would contain the new file cannot be expanded; the file that was to be created does not exist. 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 this special file does not exist. EROFS The named file resides on a read-only file system and mode requires write access. EINVAL The value of the mode argument is not valid. ENOMEM Insufficient storage space is available. ETXTBSY The file is a pure procedure (shared text) file that is being executed and mode requires write access. The fdopen subroutine fails if one or more of the following is true: EBADF The fildes argument is not a valid file descriptor. EINVAL The mode argument is not a valid mode. ENOMEM Insufficient space to allocate a buffer. RELATED INFORMATION In this book: "fclose, fflush," "fseek, rewind, ftell," "open, openx, creat," "setbuf, setvbuf," and "stdio." Processed July 12, 1991 FOPEN(3s,L) 3