Borland C++ reference

fopen

checked against scan

Opens a stream.

Defined in header <stdio.h>

Syntax

#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

Portability

DOSUNIXWindowsANSI CC++ only
■■■■

Remarks

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:

ModeDescription
rOpen for reading only.
wCreate for writing. If a file by that name already existsit will be overwritten.
aAppend; open for writing at end of fileor 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 existsit will be overwritten.
a+Open for append; open for update at the end of the fileor create if the file does not exist.

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_BINARY, 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 followed directly by input without an intervening fseek or rewind, and 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.

See also

Example

/* program to create backup of the AUTOEXEC.BAT file */

#include <stdio.h>

int main(void)
{
   FILE *in, *out;
   if ((in = fopen("\\AUTOEXEC.BAT", "rt")) == NULL) {
      fprintf(stderr, "Cannot open input file.\n");
      return 1;
   }
   if ((out = fopen("\\AUTOEXEC.BAK", "wt")) == NULL) {
      fprintf(stderr, "Cannot open output file.\n");
      return 1;
   }
   while (!feof(in))
      fputc(fgetc(in), out);
   fclose(in);
   fclose(out);
   return 0;
}

Differences from modern implementations

Draft from general knowledge; not yet confirmed against the Borland run-time code.

  • Text and binary mode. In text mode the DOS run-time translates CR LF to LF on input and LF to CR LF on output. POSIX has no such distinction and ignores b. The t mode character is a DOS/Windows extension (Microsoft’s CRT still accepts it); ISO C only defines b.
  • Default mode comes from _fmode (text unless the program changes it). Modern Microsoft CRT keeps the same mechanism; POSIX has no equivalent.
  • No x (exclusive create) mode, which ISO C11 added, and none of the Microsoft extensions such as c, n, S, R, T, D or ccs=.
  • File names are DOS paths: 8.3 names, \ as separator (written "\\" in C strings), drive letters.

Modern references