Names specified here
Name Description Notes Source Availability
FOPEN_MAX Maximum number of open files M <stdio.h> C89 C90 C95 C99 C11
fopen() Open file as stream (·) <stdio.h> C89 C90 C95 C99 C11
fopen_s() Open file as stream (·) <stdio.h> C11
freopen() Re-open stream (·) <stdio.h> C89 C90 C95 C99 C11
freopen_s() Re-open stream (·) <stdio.h> C11
#include <stdio.h>
FILE *fopen(const char *name, const char *mode);
FILE *freopen(const char *name, const char *mode, FILE *stream);

A new stream is opened and associated with a file using fopen, which returns NULL on failure. name is the name of the file, and mode indicates how to access it:

mode Format Read Write Initial position If file exists If file does not exist
r Text Yes Start Fail
rb Binary Yes Start Fail
r+ Text Yes Yes Start Fail
rb+ or r+b Binary Yes Yes Start Fail
w Text Yes Start Truncate Create
wb Binary Yes Start Truncate Create
w+ Text Yes Yes Start Truncate Create
wb+ or w+b Binary Yes Yes Start Truncate Create
wx Text Yes Start Fail Create
wbx Binary Yes Start Fail Create
w+x Text Yes Yes Start Fail Create
wb+x or w+bx Binary Yes Yes Start Fail Create
a Text Yes End Create
ab Binary Yes End Create
a+ Text Yes Yes End Create
ab+ or a+b Binary Yes Yes End Create

freopen takes an existing stream, closes it, and re-opens it on another file, returning stream on success, and NULL on failure. It is most useful for redirecting streams such as stdin, stdout and stderr to other files.

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
errno_t fopen_s(FILE **pstr, const char *name, const char *mode);
errno_t freopen_s(FILE **pstr, const char *name, const char *mode, FILE *stream);

From C11, fopen_s and freopen_s are provided. These differ from fopen and freopen in that they do not return stream handles, but instead write the new handles to *pstr if pstr is not null. On error, they set *pstr to null and return non-zero.

fopen and freopen also influence permissions on newly created files. Such files will not be accessible to ‘others’, for example, other users, if the environment has such a notion. There are also twelve additional values for mode. A prefix of u on all modes that support file creation indicates that the created file will have default permissions by the time the stream is closed.

Upto FOPEN_MAX streams can be open at once.


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000