📄 undofreopen.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file stdio.sgml dated Wed Dec 21 13:07:57 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/stdio/undofreopen.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:58 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 12.34</title><link href="freopen.html" rev=precedes><link href="redirp.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="freopen.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="redirp.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 12.34</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Once I've used <TT>freopen</TT>, how can I get the original<TT>stdout</TT>(or <TT>stdin</TT>)back?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There isn't a good way.If you need to switch back,thebest solutionis not tohave used<TT>freopen</TT> in thefirst place.Try using your own explicit output (or input) stream variable,which you can reassign at will, while leaving the original<TT>stdout</TT> (or <TT>stdin</TT>) undisturbed.For example, declare a global<pre> FILE *ofp;</pre>and replace all calls to <TT>printf( </TT>...<TT> )</TT> with<TT>fprintf(ofp, </TT>...<TT> )</TT>.(Obviously, you'll have to check for calls to<TT>putchar</TT> and <TT>puts</TT>, too.)Then you can set <TT>ofp</TT> to <TT>stdout</TT>orto anything else.</p><p>You might wonder if you could skip <TT>freopen</TT> entirely,and do something like<pre> FILE *savestdout = stdout; stdout = fopen(file, "w"); /* WRONG */</pre>leaving yourself able to restore <TT>stdout</TT> later by doing<pre> stdout = savestdout; /* WRONG */</pre>but code like this is not likely to work,because <TT>stdout</TT>(and <TT>stdin</TT> and <TT>stderr</TT>)aretypically constants which cannot be reassigned(whichis why <TT>freopen</TT> exists in the first place).</p><p>It may be possible,in a nonportable way,to save awayinformation about a streambeforecalling <TT>freopen</TT>to open some file in its place,such that the original stream can later be restored.The most straightforward and reliable way is to manipulate theunderlying file descriptors using a system-specific call such as<TT>dup</TT>or<TT>dup2</TT>,if available (<a href="rd.kirby.c">example</a>).Another is tocopy or inspect the contents of the<TT>FILE</TT>structure,but this is exceedinglynonportableand unreliable.</p><p>Under some systems,you might be able to reopen a special device file(such as <TT>/dev/fd/1</TT> under modern versions of Unix)which is still attached to (for example) the original standard output.You can,under some systems,explicitly re-open the controlling terminal(see question <a href="devtty.html">12.36</a>),but this isn't necessarily what you want,since theoriginalinput or output(i.e. what <TT>stdin</TT> or <TT>stdout</TT> had beenbefore you called <TT>freopen</TT>)could have been redirectedfrom the command line.</p><p>All of this pertains to stdio redirectioninitially performed by <TT>freopen</TT>,affecting the I/O callswithin the same program that called <TT>freopen</TT>.If what you're trying to do is capture the result of a subprogramexecution,<TT>freopen</TT> probably won't workanyway;see question <a href="../osdep/popen.html">19.30</a> instead.</p><p>Additional links:<a href="sd13.html" rel=subdocument>examples</a></p><!-- aend --><p><hr><a href="freopen.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="redirp.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/stdio/undofreopen.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:58 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -