{"id":5406,"date":"2022-12-20T18:37:24","date_gmt":"2022-12-20T21:37:24","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/freopen-man3p\/"},"modified":"2022-12-20T18:37:24","modified_gmt":"2022-12-20T21:37:24","slug":"freopen-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/freopen-man3p\/","title":{"rendered":"FREOPEN (man3p)"},"content":{"rendered":"<h1 align=\"center\">FREOPEN<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <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=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">freopen \u2014 open a stream<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <stdio.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">FILE *freopen(const char *restrict <i>pathname<\/i>, const char *restrict <i>mode<\/i>, <br \/> FILE *restrict <i>stream<\/i>);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The functionality described on this reference page is aligned with the ISO\u00a0C standard. Any conflict between the requirements described here and the ISO\u00a0C standard is unintentional. This volume of POSIX.1-2017 defers to the ISO\u00a0C standard.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>freopen<\/i>() function shall first attempt to flush the stream associated with <i>stream<\/i> as if by a call to <i>fflush<\/i>(<i>stream<\/i>). Failure to flush the stream successfully shall be ignored. If <i>pathname<\/i> is not a null pointer, <i>freopen<\/i>() shall close any file descriptor associated with <i>stream<\/i>. Failure to close the file descriptor successfully shall be ignored. The error and end-of-file indicators for the stream shall be cleared.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>freopen<\/i>() function shall open the file whose pathname is the string pointed to by <i>pathname<\/i> and associate the stream pointed to by <i>stream<\/i> with it. The <i>mode<\/i> argument shall be used just as in <i>fopen<\/i>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The original stream shall be closed regardless of whether the subsequent open succeeds.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>pathname<\/i> is a null pointer, the <i>freopen<\/i>() function shall attempt to change the mode of the stream to that specified by <i>mode<\/i>, as if the name of the file currently associated with the stream had been used. In this case, the file descriptor associated with the stream need not be closed if the call to <i>freopen<\/i>() 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\">After a successful call to the <i>freopen<\/i>() function, the orientation of the stream shall be cleared, the encoding rule shall be cleared, and the associated <b>mbstate_t<\/b> object shall be set to describe an initial conversion state.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>pathname<\/i> is not a null pointer, or if <i>pathname<\/i> is a null pointer and the specified mode change necessitates the file descriptor associated with the stream to be closed and reopened, the file descriptor associated with the reopened stream shall be allocated and opened as if by a call to <i>open<\/i>() with the following flags:<\/p>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-849741.png\" alt=\"Image grohtml-849741.png\"><\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, <i>freopen<\/i>() shall return the value of <i>stream<\/i>. Otherwise, a null pointer shall be returned, and <i>errno<\/i> shall be set to indicate the error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>freopen<\/i>() function shall fail if:<\/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=\"9%\">\n<p><b>EACCES<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by <i>mode<\/i> are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EBADF<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The file descriptor underlying the stream is not a valid file descriptor when <i>pathname<\/i> is a null pointer.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINTR<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>A signal was caught during <i>freopen<\/i>().<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EISDIR<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The named file is a directory and <i>mode<\/i> requires write access.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>A loop exists in symbolic links encountered during resolution of the <i>path<\/i> argument.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EMFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>All file descriptors available to the process are currently open.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">The length of a component of a pathname is longer than {NAME_MAX}.<\/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=\"9%\">\n<p><b>ENFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The maximum allowable number of files is currently open in the system.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOENT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The <i>mode<\/i> string begins with <b>\u2019r\u2019<\/b> and a component of <i>pathname<\/i> does not name an existing file, or <i>mode<\/i> begins with <b>\u2019w\u2019<\/b> or <b>\u2019a\u2019<\/b> and a component of the path prefix of <i>pathname<\/i> does not name an existing file, or <i>pathname<\/i> is an empty string.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOENT<\/b> or <b>ENOTDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">The <i>pathname<\/i> argument contains at least one non-<slash> character and ends with one or more trailing <slash> characters. If <i>pathname<\/i> without the trailing <slash> characters would name an existing file, an <b>[ENOENT]<\/b> error shall not occur.<\/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=\"9%\">\n<p><b>ENOSPC<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The directory or file system that would contain the new file cannot be expanded, the file does not exist, and it was to be created.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOTDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">A component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory, or the <i>pathname<\/i> argument contains at least one non-<slash> character and ends with one or more trailing <slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.<\/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=\"7%\">\n<p><b>ENXIO<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>The named file is a character special or block special file, and the device associated with this special file does not exist.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EOVERFLOW<\/b><\/p>\n<p style=\"margin-left:22%;\">The named file is a regular file and the size of the file cannot be represented correctly in an object of type <b>off_t<\/b>.<\/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=\"7%\">\n<p><b>EROFS<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>The named file resides on a read-only file system and <i>mode<\/i> requires write access.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>freopen<\/i>() function may fail if:<\/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=\"9%\">\n<p style=\"margin-top: 1em\"><b>EBADF<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">The mode with which the file descriptor underlying the stream was opened does not support the requested mode when <i>pathname<\/i> is a null pointer.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The value of the <i>mode<\/i> argument is not valid.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>path<\/i> argument.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.<\/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=\"9%\">\n<p><b>ENOMEM<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Insufficient storage space is available.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENXIO<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>A request was made of a nonexistent device, or the request was outside the capabilities of the device.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ETXTBSY<\/b><\/p>\n<p style=\"margin-left:22%;\">The file is a pure procedure (shared text) file that is being executed and <i>mode<\/i> requires write access.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i><b>Directing Standard Output to a File<\/b><\/i> <br \/> The following example logs all standard output to the <b>\/tmp\/logfile<\/b> file.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <stdio.h> <br \/> &#8230; <br \/> FILE *fp; <br \/> &#8230; <br \/> fp = freopen (&#8220;\/tmp\/logfile&#8221;, &#8220;a+&#8221;, stdout); <br \/> &#8230;<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>freopen<\/i>() function is typically used to attach the pre-opened <i>streams<\/i> associated with <i>stdin<\/i>, <i>stdout<\/i>, and <i>stderr<\/i> to other files.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Since implementations are not required to support any stream mode changes when the <i>pathname<\/i> argument is NULL, portable applications cannot rely on the use of <i>freopen<\/i>() to change the stream mode, and use of this feature is discouraged. The feature was originally added to the ISO\u00a0C standard in order to facilitate changing <i>stdin<\/i> and <i>stdout<\/i> to binary mode. Since a <b>\u2019b\u2019<\/b> character in the mode has no effect on POSIX systems, this use of the feature is unnecessary in POSIX applications. However, even though the <b>\u2019b\u2019<\/b> is ignored, a successful call to <i>freopen<\/i>(NULL, &#8220;<i>wb<\/i>&#8220;, <i>stdout<\/i>) does have an effect. In particular, for regular files it truncates the file and sets the file-position indicator for the stream to the start of the file. It is possible that these side-effects are an unintended consequence of the way the feature is specified in the ISO\/IEC\u00a09899:1999 standard, but unless or until the ISO\u00a0C standard is changed, applications which successfully call <i>freopen<\/i>(NULL, &#8220;<i>wb<\/i>&#8220;, <i>stdout<\/i>) will behave in unexpected ways on conforming systems in situations such as:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">{ appl file1; appl file2; } > file3<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">which will result in <b>file3<\/b> containing only the output from the second invocation of <i>appl<\/i>.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Section 2.5<\/i>, <i>Standard I\/O Streams<\/i>, <i>fclose<\/i>(), <i>fdopen<\/i>(), <i>fflush<\/i>(), <i>fmemopen<\/i>(), <i>fopen<\/i>(), <i>mbsinit<\/i>(), <i>open<\/i>(), <i>open_memstream<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><stdio.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  freopen \u2014 open a stream <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[2295,1594],"class_list":["post-5406","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-freopen","tag-man3p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5406","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=5406"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5406\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}