fopen(3) Library Functions Manual fopen(3) fopen, fdopen, freopen - LIBRARY Standard C library (libc, -lc) #include FILE *fopen(const char *restrict pathname, const char *restrict mode); FILE *fdopen(int fd, const char *mode); FILE *freopen(const char *restrict pathname, const char *restrict mode, FILE *restrict stream); glibc (. feature_test_macros(7)): fdopen(): _POSIX_C_SOURCE fopen() , pathname, . mode , ( , ): r . . r+ . . w . . w+ . , , . . a ( ). , . . a+ ( ). , . . POSIX , . glibc , Android/BSD/MacOS . The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with ISO C and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the 'b' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.) mode glibc . S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH (0666), umask ( umask(2)). Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek(3) or fsetpos(3) operation between write and read operations on such a stream. This operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect). Opening a file in append mode (a as the first character of mode) causes all subsequent write operations to this stream to occur at end-of-file, as if preceded by the call: fseek(stream, 0, SEEK_END); , , open(2) : +------------------------+-------------------------------+ | fopen() | open() | +------------------------+-------------------------------+ | r | O_RDONLY | +------------------------+-------------------------------+ | w | O_WRONLY | O_CREAT | O_TRUNC | +------------------------+-------------------------------+ | a | O_WRONLY | O_CREAT | O_APPEND | +------------------------+-------------------------------+ | r+ | O_RDWR | +------------------------+-------------------------------+ | w+ | O_RDWR | O_CREAT | O_TRUNC | +------------------------+-------------------------------+ | a+ | O_RDWR | O_CREAT | O_APPEND | +------------------------+-------------------------------+ fdopen() fdopen() fd. mode ( : <>, <>, <>, ,w+>>, <>, <>) . , fd, . <> <> . , fdopen(). fdopen() . freopen() freopen() pathname , stream. ( ) . mode fopen(). pathname null, freopen() mode; , freopen() pathname, . C99, : , , , freopen() . . freopen() , (stderr, stdin stdout). Upon successful completion fopen(), fdopen(), and freopen() return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error. EINVAL mode fopen(), fdopen() freopen(). The fopen(), fdopen(), and freopen() functions may also fail and set errno for any of the errors specified for the routine malloc(3). fopen() errno - open(2). fdopen() errno - fcntl(2). freopen() errno - open(2), fclose(3) fflush(3). attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |fopen(), fdopen(), | | MT-Safe | |freopen() | | | +----------------------------+----------------------------------------------------------+--------------------------+ fopen() freopen() C11, POSIX.1-2008. fdopen() POSIX.1-2008. fopen() freopen() POSIX.1-2001, C89. fdopen() POSIX.1-2001. glibc GNU C mode: c ( glibc 2.3.3) Do not make the open operation, or subsequent read and write operations, thread cancelation points. This flag is ignored for fdopen(). e ( glibc 2.7) O_CLOEXEC. open(2). fdopen(). m ( glibc 2.3) mmap(2), - (read(2), write(2)). mmap(2) , . x ( O_EXCL open(2)). , fopen() errno EEXIST. fdopen(). , fopen() freopen() mode: ,ccs= . - . ,ccs= , . , . When parsing for individual flag characters in mode (i.e., the characters preceding the "ccs" specification), the glibc implementation of fopen() and freopen() limits the number of characters examined in mode to 7 (or, before glibc 2.14, to 6, which was not enough to include possible specifications such as "rb+cmxe"). The current implementation of fdopen() parses at most 5 characters in mode. . open(2), fclose(3), fileno(3), fmemopen(3), fopencookie(3), open_memstream(3) Azamat Hackimov , Dmitry Bolkhovskikh , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . fopen(3)