RAD Studio |  |

fopen, _wfopen

Header File

stdio.h

Category

Input/output Routines

Prototype

FILE *fopen(const char *filename, const char *mode);

FILE *_wfopen(const wchar_t *filename, const wchar_t *mode);

Description

Opens a stream.

fopen opens the file named by filename and associates a stream with it. fopen returns a pointer to be used to identify the stream in subsequent operations.

The mode string used in calls to fopen is one of the following values:

r | Open for reading only.
w | Create for writing. If a file by that name already exists, it will be overwritten.
a | Append; open for writing at end-of-file or create for writing if the file does not exist.
r+ | Open an existing file for update (reading and writing).
w+ | Create a new file for update (reading and writing). If a file by that name already exists, it will be overwritten.
a+ | Open for append; open (or create if the file does not exist) for update at the end of the file.

To specify that a given file is being opened or created in text mode append a t to the mode string (rt w+t and so on). Similarly to specify binary mode append a b to the mode string (wb a+b and so on). fopen also allows the t or b to be inserted between the letter and the + character in the mode string; for example rt+ is equivalent to r+t.

If a t or b is not given in the mode string the mode is governed by the global variable _fmode. If _fmode is set to O_B/INARY files are opened in binary mode. If _fmode is set to O_TEXT they are opened in text mode. These O_... constants are defined in fcntl.h.

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 fseek or rewind
- input cannot be directly followed by output without an intervening fseek, rewind, or an input that encounters end-of-file

Return Value

On successful completion fopen returns a pointer to the newly opened stream. In the event of error it returns NULL.

Portability

| POSIX | Win32 | ANSI C | ANSI C++
fopen | + | + | + | +
_wfopen |  | NT only |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!