Redirecting the Standard Streams


As mentioned earlier, the standard streams, such as Console.In, can be redirected. By far, the most common redirection is to a file. When a standard stream is redirected, input and/ or output is automatically directed to the new stream, bypassing the default devices. By redirecting the standard streams, your program can read commands from a disk file, create log files, or even read input from a network connection.

Redirection of the standard streams can be accomplished in two ways. First, when you execute a program on the command line, you can use the < and > operators to redirect Console.In and/or Console.Out, respectively. For example, given this program:

 using System; class Test {   public static void Main() {     Console.WriteLine("This is a test.");   } }

executing the program like this,

 Test > log

will cause the line “This is a test.” to be written to a file called log. Input can be redirected in the same way. The thing to remember when input is redirected is that you must make sure that what you specify as an input source contains sufficient input to satisfy the demands of the program. If it doesn’t, the program will hang.

The < and > command-line redirection operators are not part of C#, but are provided by the operating system. Thus, if your environment supports I/O redirection (as is the case with Windows), you can redirect standard input and standard output without making any changes to your program. However, there is a second way that you can redirect the standard streams that is under program control. To do so, you will use the SetIn( ), SetOut( ), and SetError( ) methods, shown here, which are members of Console:

 static void SetIn(TextReader input) static void SetOut(TextWriter output) static void SetError(TextWriter output)

Thus, to redirect input, call SetIn( ), specifying the desired stream. You can use any input stream as long as it is derived from TextReader. For example, to redirect output to a file, specify a FileStream that is wrapped in a StreamWriter. The following program shows an example:

 // Redirect Console.Out. using System; using System.IO; class Redirect {   public static void Main() {     StreamWriter log_out;     try {       log_out = new StreamWriter("logfile.txt");     }     catch(IOException exc) {       Console.WriteLine(exc.Message + "Cannot open file.");       return ;     }     // Direct standard output to the log file.     Console.SetOut(log_out);     Console.WriteLine("This is the start of the log file.");     for(int i=0; i<10; i++) Console.WriteLine(i);     Console.WriteLine("This is the end of the log file.");     log_out.Close();   } }

When you run this program, you won’t see any of the output on the screen, but the file logfile.txt will contain the following:

 This is the start of the log file. 0 1 2 3 4 5 6 7 8 9 This is the end of the log file.

On your own, you might want to experiment with redirecting the other built-in streams.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net