Console::OpenStandardOutput Method (Int32)
.NET Framework (current version)
Acquires the standard output stream, which is set to a specified buffer size.
Assembly: mscorlib (in mscorlib.dll)
public: [HostProtectionAttribute(SecurityAction::LinkDemand, UI = true)] static Stream^ OpenStandardOutput( int bufferSize )
Parameters
- bufferSize
-
Type:
System::Int32
The internal stream buffer size.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | bufferSize is less than or equal to zero. |
This method can be used to reacquire the standard output stream after it has been changed by the SetOut method.
The following example illustrates the use of the OpenStandardOutput method. It replaces four consecutive space characters in a string with a tab character. To run it, you must supply two command line arguments. The first is the name of an existing text file to redirect the standard input stream to. The second is the name of a file to redirect the standard output stream to. This file need not exist. If it does, its contents will be overwritten.
using namespace System; using namespace System::IO; int main() { array<String^>^args = Environment::GetCommandLineArgs(); const int tabSize = 4; String^ usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt"; StreamWriter^ writer = nullptr; if ( args->Length < 3 ) { Console::WriteLine( usageText ); return 1; } try { // Attempt to open output file. writer = gcnew StreamWriter( args[ 2 ] ); // Redirect standard output from the console to the output file. Console::SetOut( writer ); // Redirect standard input from the console to the input file. Console::SetIn( gcnew StreamReader( args[ 1 ] ) ); } catch ( IOException^ e ) { TextWriter^ errorWriter = Console::Error; errorWriter->WriteLine( e->Message ); errorWriter->WriteLine( usageText ); return 1; } String^ line; while ( (line = Console::ReadLine()) != nullptr ) { String^ newLine = line->Replace( ((String^)"")->PadRight( tabSize, ' ' ), "\t" ); Console::WriteLine( newLine ); } writer->Close(); // Recover the standard output stream so that a // completion message can be displayed. StreamWriter^ standardOutput = gcnew StreamWriter( Console::OpenStandardOutput() ); standardOutput->AutoFlush = true; Console::SetOut( standardOutput ); Console::WriteLine( "INSERTTABS has completed the processing of {0}.", args[ 1 ] ); return 0; }
.NET Framework
Available since 1.1
Available since 1.1
Show: