delorie.com/djgpp/doc/libc/libc_337.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

fopen

Syntax

 
#include <stdio.h>

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

Description

This function opens a stream corresponding to the named filename with the given mode. The mode can be one of the following:

r

Open an existing file for reading.

w

Create a new file (or truncate an existing file) and open it for writing.

a

Open an existing file (or create a new one) for writing. The file pointer is positioned to the end of the file before every write.

Followed by any of these characters:

b

Force the file to be open in binary mode instead of the default mode.

When called to open the console in binary mode, fopen will disable the generation of SIGINT when you press Ctrl-C (Ctrl-Break will still cause SIGINT), because many programs that use binary reads from the console will also want to get the `^C' characters. You can use the __djgpp_set_ctrl_c library function (see section __djgpp_set_ctrl_c) if you want Ctrl-C to generate interrupts while console is read in binary mode.

t

Force the file to be open in text mode instead of the default mode.

+

Open the file as with O_RDWR so that both reads and writes can be done to the same file.

If the file is open for both reading and writing, you must call fflush, fseek, or rewind before switching from read to write or from write to read.

The open file is set to line buffered if the underlying object is a device (stdin, stdout, etc), or is fully buffered if the underlying object is a disk file (data.c, etc).

If b or t is not specified in mode, the file type is chosen by the value of fmode (see section _fmode).

You can open directories using fopen, but there is limited support for stream file operations on directories. In particular, they cannot be read from or written to.

If you need to specify the DOS share flags use the __djgpp_share_flags. See section __djgpp_share_flags.

Return Value

A pointer to the FILE object, or NULL if there was an error.

Portability

ANSI/ISO C C89; C99
POSIX 1003.2-1992; 1003.1-2001

Example

 
FILE *f = fopen("foo", "rb+"); /* open existing file for read/write,
                                * binary mode */


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004