GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 26 of 457    Print  
How do you redirect a standard stream?
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:>PRINTIT < STRINGS.TXT
Notice that the name of the executable file always comes first. The less-than sign (<) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program. 
The following example would redirect the program’s output to the prn device, usually the printer attached on LPT1:
C :> REDIR > PRN
Alternatively, you might want to redirect the program’s output to a file, as the following example shows:
C :> REDIR > REDIR.OUT
In this example, all output that would have normally appeared on-screen will be written to the file
REDIR.OUT.
 
Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from within your program 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.
 


  
Total Answers and Comments: 3 Last Update: January 30, 2010   
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
August 13, 2007 09:21:08   
Lance Norteno        

RE: How do you redirect a standard stream? ...
How you redirect your output depends on your operating system. Basically when your program finishes your output goes to a sysout queue. From there it has to be picked up. I have seen sites where they poll the sysout and send it through Adobe or through a pipe to the printer or even to hard drive. There are even ways to log on from your terminal and manually redirect jobs to their destination. Not knowing what type of configuration you have it would be hard to be more specific.
 
Is this answer useful? Yes | No
June 09, 2008 07:46:26   
jintojos Member Since: May 2008   Contribution: 29    

RE: How do you redirect a standard stream?
By using the operators "<" and ">" we can redirect the standard input and out streams.

example : d:> jinto.exe > outputredirected

d:> jinto.exe < inputredirected

 
Is this answer useful? Yes | No
January 30, 2010 01:55:27   
abhimanipal Member Since: July 2009   Contribution: 48    

RE: How do you redirect a standard stream?
Another way to do this is to use the dup system call.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape