Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirction of stdout and "unredirecting" 1

Status
Not open for further replies.

emart

Programmer
Apr 26, 2000
11
0
0
US
<br>I have some frequent and simple C questions like the one below.&nbsp;&nbsp;Are they appropriate for this forum? <br><br>Are there disadvantages to redirecting standard output to a file?&nbsp;&nbsp;And How do I undo the redirection so I can print to the screen again later in the program?<br><br>#include &lt;stdio.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;// standard io:<br>#include &lt;stdlib.h&gt;&nbsp;&nbsp;&nbsp;// standard functions: system<br>main(int argc, char *argv[]) {<br>&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;rc=8;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// return code<br>&nbsp;&nbsp;// redirects standard output to a file<br>&nbsp;&nbsp;freopen(&quot;FILETEST&quot;, &quot;w&quot;, stdout);&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;This line goes to FILETEST&quot;);<br><br>&nbsp;&nbsp;// unredirect.&nbsp;&nbsp;How do I do it?<br><br>&nbsp;&nbsp;printf(&quot;This line goes to the screen&quot;);<br>}<br>
 
I don't know how you do that, but can't you just do the below instead???<br><br>#include &lt;stdio.h&gt;<br><br>int main(void)<br>{<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;FILE *stream;<br>&nbsp;&nbsp;&nbsp;int i = 100;<br>&nbsp;&nbsp;&nbsp;char c = 'C';<br>&nbsp;&nbsp;&nbsp;float f = 1.234;<br><br>&nbsp;&nbsp;&nbsp;/* open a file for update */<br>&nbsp;&nbsp;&nbsp;stream = fopen(&quot;DUMMY.FIL&quot;, &quot;w+&quot;);<br><br>&nbsp;&nbsp;&nbsp;/* write some data to the file */<br>&nbsp;&nbsp;&nbsp;fprintf(stream, &quot;The data is: %d %c %f&quot;, i, c, f);<br><br>&nbsp;&nbsp;&nbsp;/* close the file */<br>&nbsp;&nbsp;&nbsp;fclose(stream);<br><br>&nbsp;&nbsp;&nbsp;/* write some data to the screen */<br>&nbsp;&nbsp;&nbsp;printf(&quot;The data is: %d %c %f&quot;, i, c, f);<br>&nbsp;&nbsp;&nbsp;return 0;<br>} <p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
Actually when you use any file related function . The system just opens a stream. You need to close it if you don't want it any more.<br>Do this ?<br><br>FILE stream;<br>stream = freopen(&quot;FILETEST&quot;, &quot;w&quot;, stdout);&nbsp;&nbsp;<br>fclose(stream); // This will close the stream.<br>fprintf(stdout,&quot;Now it is standard output&quot;);<br><br>// you are done<br><br>Thanx<br>Siddhartha Singh.<br>
 
Thanks for your help SSingh,&nbsp;&nbsp;Could you look at my other question about exec, spawn, and system?&nbsp;&nbsp;I use Power C 2.2.1 which seems to be a DOS compiler.&nbsp;&nbsp;When I build a command string in C and try to submit it, am I limited to 128 characters because of the compiler.&nbsp;&nbsp;The command line I want to submit is 400 charaters!&nbsp;&nbsp;How do I do it?<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top