CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How do you redirect a standard stream?
Category C Interview Questions
Rating (5.0) By 1 users
Added on 10/22/2004
Views 2854
Rate it!
Answers:

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.





If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
How can you restore a redirected standard stream?
View Answer
What is the difference between text and binary modes?
View Answer
How do you determine whether to use a stream function or a low-level function?
View Answer
How can I open a file so that other programs can update it at the same time?
View Answer
How can I make sure that my program is the only one accessing a file?
View Answer
What is Preprocessor?
View Answer
What is a macro, and how do you use it?
View Answer
What will the preprocessor do for a program?
View Answer
How can you avoid including a header more than once?
View Answer
Can a file other than a .h file be included with #include?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/1040/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.61