{"id":6971,"date":"2022-12-20T19:34:33","date_gmt":"2022-12-20T22:34:33","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/fopen-man3\/"},"modified":"2022-12-20T19:34:33","modified_gmt":"2022-12-20T22:34:33","slug":"fopen-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/fopen-man3\/","title":{"rendered":"FOPEN (man3)"},"content":{"rendered":"<h1 align=\"center\">FOPEN<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">fopen, fdopen, freopen \u2212 stream open functions<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <stdio.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>FILE *fopen(const char *<\/b><i>pathname<\/i><b>, const char *<\/b><i>mode<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>FILE *fdopen(int<\/b> <i>fd<\/i><b>, const char *<\/b><i>mode<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>FILE *freopen(const char *<\/b><i>pathname<\/i><b>, const char *<\/b><i>mode<\/i><b>, FILE *<\/b><i>stream<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:5%; margin-top: 1em\">Feature Test Macro Requirements for glibc (see <b>feature_test_macros<\/b>(7)):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fdopen<\/b>(): _POSIX_C_SOURCE<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>fopen<\/b>() function opens the file whose name is the string pointed to by <i>pathname<\/i> and associates a stream with it.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The argument <i>mode<\/i> points to a string beginning with one of the following sequences (possibly followed by additional characters, as described below):<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p><b>r<\/b><\/p>\n<\/td>\n<td width=\"8%\"><\/td>\n<td width=\"78%\">\n<p>Open text file for reading. The stream is positioned at the beginning of the file.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p><b>r+<\/b><\/p>\n<\/td>\n<td width=\"8%\"><\/td>\n<td width=\"78%\">\n<p>Open for reading and writing. The stream is positioned at the beginning of the file.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p><b>w<\/b><\/p>\n<\/td>\n<td width=\"8%\"><\/td>\n<td width=\"78%\">\n<p>Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p><b>w+<\/b><\/p>\n<\/td>\n<td width=\"8%\"><\/td>\n<td width=\"78%\">\n<p>Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p><b>a<\/b><\/p>\n<\/td>\n<td width=\"8%\"><\/td>\n<td width=\"78%\">\n<p>Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p><b>a+<\/b><\/p>\n<\/td>\n<td width=\"8%\"><\/td>\n<td width=\"78%\">\n<p>Open for reading and appending (writing at end of file). The file is created if it does not exist. Output is always appended to the end of the file. POSIX is silent on what the initial read position is when using this mode. For glibc, the initial file position for reading is at the beginning of the file, but for Android\/BSD\/MacOS, the initial file position for reading is at the end of the file.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>mode<\/i> string can also include the letter &#8216;b&#8217; 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 C89 and has no effect; the &#8216;b&#8217; is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the &#8216;b&#8217; 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.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">See NOTES below for details of glibc extensions for <i>mode<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any created file will have the mode <b>S_IRUSR<\/b> | <b>S_IWUSR<\/b> | <b>S_IRGRP<\/b> | <b>S_IWGRP<\/b> | <b>S_IROTH<\/b> | <b>S_IWOTH<\/b> (0666), as modified by the process\u2019s umask value (see <b>umask<\/b>(2)).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <b>fseek<\/b>(3) or <b>fgetpos<\/b>(3) operation between write and read operations on such a stream. This operation may be an apparent no-op (as in <i>fseek(&#8230;, 0L, SEEK_CUR)<\/i> called for its synchronizing side effect).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Opening a file in append mode (<b>a<\/b> as the first character of <i>mode<\/i>) causes all subsequent write operations to this stream to occur at end-of-file, as if preceded the call:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">fseek(stream, 0, SEEK_END);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The file descriptor associated with the stream is opened as if by a call to <b>open<\/b>(2) with the following flags:<\/p>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-1640201.png\" alt=\"Image grohtml-1640201.png\"><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fdopen()<\/b> <br \/> The <b>fdopen<\/b>() function associates a stream with the existing file descriptor, <i>fd<\/i>. The <i>mode<\/i> of the stream (one of the values &#8220;r&#8221;, &#8220;r+&#8221;, &#8220;w&#8221;, &#8220;w+&#8221;, &#8220;a&#8221;, &#8220;a+&#8221;) must be compatible with the mode of the file descriptor. The file position indicator of the new stream is set to that belonging to <i>fd<\/i>, and the error and end-of-file indicators are cleared. Modes &#8220;w&#8221; or &#8220;w+&#8221; do not cause truncation of the file. The file descriptor is not dup\u2019ed, and will be closed when the stream created by <b>fdopen<\/b>() is closed. The result of applying <b>fdopen<\/b>() to a shared memory object is undefined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>freopen()<\/b> <br \/> The <b>freopen<\/b>() function opens the file whose name is the string pointed to by <i>pathname<\/i> and associates the stream pointed to by <i>stream<\/i> with it. The original stream (if it exists) is closed. The <i>mode<\/i> argument is used just as in the <b>fopen<\/b>() function.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the <i>pathname<\/i> argument is a null pointer, <b>freopen<\/b>() changes the mode of the stream to that specified in <i>mode<\/i>; that is, <b>freopen<\/b>() reopens the pathname that is associated with the stream. The specification for this behavior was added in the C99 standard, which says:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">In this case, the file descriptor associated with the stream need not be closed if the call to <b>freopen<\/b>() succeeds. It is implementation-defined which changes of mode are permitted (if any), and under what circumstances.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The primary use of the <b>freopen<\/b>() function is to change the file associated with a standard text stream (<i>stderr<\/i>, <i>stdin<\/i>, or <i>stdout<\/i>).<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion <b>fopen<\/b>(), <b>fdopen<\/b>(), and <b>freopen<\/b>() return a <i>FILE<\/i> pointer. Otherwise, NULL is returned and <i>errno<\/i> is set to indicate the error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p style=\"margin-top: 1em\"><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">The <i>mode<\/i> provided to <b>fopen<\/b>(), <b>fdopen<\/b>(), or <b>freopen<\/b>() was invalid.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>fopen<\/b>(), <b>fdopen<\/b>(), and <b>freopen<\/b>() functions may also fail and set <i>errno<\/i> for any of the errors specified for the routine <b>malloc<\/b>(3).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>fopen<\/b>() function may also fail and set <i>errno<\/i> for any of the errors specified for the routine <b>open<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>fdopen<\/b>() function may also fail and set <i>errno<\/i> for any of the errors specified for the routine <b>fcntl<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>freopen<\/b>() function may also fail and set <i>errno<\/i> for any of the errors specified for the routines <b>open<\/b>(2), <b>fclose<\/b>(3), and <b>fflush<\/b>(3).<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-1640202.png\" alt=\"Image grohtml-1640202.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fopen<\/b>(), <b>freopen<\/b>(): POSIX.1-2001, POSIX.1-2008, C89, C99.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fdopen<\/b>(): POSIX.1-2001, POSIX.1-2008.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Glibc notes<\/b> <br \/> The GNU C library allows the following extensions for the string specified in <i>mode<\/i>: <b><br \/> c<\/b> (since glibc 2.3.3)<\/p>\n<p style=\"margin-left:22%;\">Do not make the open operation, or subsequent read and write operations, thread cancellation points. This flag is ignored for <b>fdopen<\/b>().<\/p>\n<p style=\"margin-left:11%;\"><b>e<\/b> (since glibc 2.7)<\/p>\n<p style=\"margin-left:22%;\">Open the file with the <b>O_CLOEXEC<\/b> flag. See <b>open<\/b>(2) for more information. This flag is ignored for <b>fdopen<\/b>().<\/p>\n<p style=\"margin-left:11%;\"><b>m<\/b> (since glibc 2.3)<\/p>\n<p style=\"margin-left:22%;\">Attempt to access the file using <b>mmap<\/b>(2), rather than I\/O system calls (<b>read<\/b>(2), <b>write<\/b>(2)). Currently, use of <b>mmap<\/b>(2) is attempted only for a file opened for reading.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p><b>x<\/b><\/p>\n<\/td>\n<td width=\"10%\"><\/td>\n<td width=\"78%\">\n<p>Open the file exclusively (like the <b>O_EXCL<\/b> flag of <b>open<\/b>(2)). If the file already exists, <b>fopen<\/b>() fails, and sets <i>errno<\/i> to <b>EEXIST<\/b>. This flag is ignored for <b>fdopen<\/b>().<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition to the above characters, <b>fopen<\/b>() and <b>freopen<\/b>() support the following syntax in <i>mode<\/i>:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>,ccs=<\/b><i>string<\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The given <i>string<\/i> is taken as the name of a coded character set and the stream is marked as wide-oriented. Thereafter, internal conversion functions convert I\/O to and from the character set <i>string<\/i>. If the <b>,ccs=<\/b><i>string<\/i> syntax is not specified, then the wide-orientation of the stream is determined by the first file operation. If that operation is a wide-character operation, the stream is marked wide-oriented, and functions to convert to the coded character set are loaded.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">When parsing for individual flag characters in <i>mode<\/i> (i.e., the characters preceding the &#8220;ccs&#8221; specification), the glibc implementation of <b>fopen<\/b>() and <b>freopen<\/b>() limits the number of characters examined in <i>mode<\/i> to 7 (or, in glibc versions before 2.14, to 6, which was not enough to include possible specifications such as &#8220;rb+cmxe&#8221;). The current implementation of <b>fdopen<\/b>() parses at most 5 characters in <i>mode<\/i>.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>open<\/b>(2), <b>fclose<\/b>(3), <b>fileno<\/b>(3), <b>fmemopen<\/b>(3), <b>fopencookie<\/b>(3), <b>open_memstream<\/b>(3)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  fopen, fdopen, freopen \u2212 stream open functions <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,1983,3007],"class_list":["post-6971","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-fopen","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6971"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6971\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}