Console.OpenStandardOutput() Method in C# with Examples
Last Updated :
22 Jun, 2020
Console.OpenStandardOutput Method is used to get the standard output stream. There are two overloads of OpenStandardOutput method available in C# which are listed below:
- OpenStandardOutput() Method
- OpenStandardOutput(int32) Method
OpenStandardOutput() Method
It is used to get the standard output stream.
Syntax: public static System.IO.Stream OpenStandardOutput ();
Parameters: This method does not accepts any parameter.
Return value: This method returns the standard output stream.
Example:
using System;
class GFG
{
static void Main( string [] args)
{
if (System.Console.OpenStandardOutput().BeginWrite( new byte [] { 071, 101,101,
107,115, 102, 111, 114, 103, 101,101,
107,115, 0 },0,
13, null , null ).AsyncWaitHandle.WaitOne())
{
}
}
}
|
Output:
Geeksforgeeks
OpenStandardOutput(Int32) Method
It is used to get the standard output stream, which is set to a specified buffer size.
Syntax:public static System.IO.Stream OpenStandardOutput (int bufferSize);
Parameters: This method accepts the following parameter.
- bufferSize: This parameter is the internal stream buffer size.
Return value: This method returns the standard output stream.
Exception: This method will give ArgumentOutOfRangeException if the buffersize is less than or equal to zero.
Example: It replaces 2 consecutive space characters in a string with a tab character.
using System;
using System.IO;
public class GFG
{
private const int Val1 = 2;
private const string Val_Text = "Geeks for Geeks" ;
public static int Main( string [] args)
{
if (args.Length < 2)
{
Console.WriteLine(Val_Text);
return 1;
}
try
{
using ( var wrt1 = new StreamWriter(args[1]))
{
using ( var rdr1 = new StreamReader(args[0]))
{
Console.SetOut(wrt1);
Console.SetIn(rdr1);
string line;
while ((line = Console.ReadLine()) != null )
{
string newLine = line.Replace(( "" ).PadRight(Val1, ' ' ), "\t" );
Console.WriteLine(newLine);
}
}
}
}
catch (IOException e)
{
TextWriter errwrt = Console.Error;
errwrt.WriteLine(e.Message);
return 1;
}
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true ;
Console.SetOut(standardOutput);
Console.WriteLine( "OpenStandardOutput Example" );
return 0;
}
}
|
Output:
Geeks for Geeks
Share your thoughts in the comments
Please Login to comment...