Wednesday, February 25, 2015

How do you redirect a standard stream?

Question:How do you redirect a standard stream??

Answer: </I>Most operating systems, including DOS, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen; it can be redirected to a file or printer port. Similarly, your program's input (stdin) can come from a file rather than the keyboard. In DOS, this task is accomplished using the redirection characters, < and >. For example, if you wanted a program named PRINTIT.EXE to receive its input (stdin) from a file named STRINGS.TXT, you would enter the following command at the DOS prompt: C:&gt;PRINTIT &lt; STRINGS.TXT Notice that the name of the executable file always comes first. The less-than sign (&lt;) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program.&nbsp; The following example would redirect the program's output to the prn device, usually the printer attached on LPT1: C :&gt; REDIR &gt; PRN Alternatively, you might want to redirect the program's output to a file, as the following example shows: C :&gt; REDIR &gt; REDIR.OUT In this example, all output that would have normally appeared on-screen will be written to the file REDIR.OUT. &nbsp;Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from <I>within your program </I>by using the standard C library function named freopen(). For example, if you wanted to redirect the stdout standard stream within your program to a file named OUTPUT.TXT, you would implement the freopen() function as shown here: ... freopen("output.txt", "w", stdout); ... Now, every output statement (printf(), puts(), putch(), and so on) in your program will appear in the file OUTPUT.TXT. &nbsp;

Tags:Interview Questions, Languages, C,

Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.



Sponsor Advertisement




Medical Billing Software

Hospital Management System Software

Hospital Management System (HMS) Software is developed for multispeciality hospitals, to cover a wide range of hospital administration and management processes with financial accounting, voucher entry, pharmacy, patients module etc features...

No comments:

Post a Comment