Console.OpenStandardOutput Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Uzyskuje standardowy strumień wyjściowy.
Przeciążenia
OpenStandardOutput() |
Uzyskuje standardowy strumień wyjściowy. |
OpenStandardOutput(Int32) |
Uzyskuje standardowy strumień wyjściowy, który jest ustawiony na określony rozmiar buforu. |
OpenStandardOutput()
Uzyskuje standardowy strumień wyjściowy.
public:
static System::IO::Stream ^ OpenStandardOutput();
public static System.IO.Stream OpenStandardOutput ();
static member OpenStandardOutput : unit -> System.IO.Stream
Public Shared Function OpenStandardOutput () As Stream
Zwraca
Standardowy strumień wyjściowy.
Przykłady
Poniższy przykład ilustruje użycie OpenStandardOutput metody . Zastępuje cztery następujące po sobie znaki spacji w ciągu zawierającym znak tabulacji. Aby go uruchomić, należy podać dwa argumenty wiersza polecenia. Pierwszy to nazwa istniejącego pliku tekstowego, do którego zostanie przekierowany standardowy strumień danych wejściowych. Drugi to nazwa pliku, do którego zostanie przekierowany standardowy strumień danych wyjściowych. Ten plik nie musi istnieć. Jeśli istnieje, jego zawartość zostanie zastąpiona.
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;
}
using System;
using System.IO;
public class InsertTabs
{
private const int tabSize = 4;
private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
public static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine(usageText);
return 1;
}
try
{
// Attempt to open output file.
using (var writer = new StreamWriter(args[1]))
{
using (var reader = new StreamReader(args[0]))
{
// 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(reader);
string line;
while ((line = Console.ReadLine()) != null)
{
string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
}
}
}
catch(IOException e)
{
TextWriter errorWriter = Console.Error;
errorWriter.WriteLine(e.Message);
errorWriter.WriteLine(usageText);
return 1;
}
// Recover the standard output stream so that a
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine($"INSERTTABS has completed the processing of {args[0]}.");
return 0;
}
}
open System
open System.IO
let tabSize = 4
let usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt"
[<EntryPoint>]
let main args =
if args.Length < 2 then
Console.WriteLine usageText
1
else
try
// Attempt to open output file.
use reader = new StreamReader(args[0])
use writer = new StreamWriter(args[1])
// 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 reader
let mutable line = Console.ReadLine()
while line <> null do
let newLine = line.Replace(("").PadRight(tabSize, ' '), "\t")
Console.WriteLine newLine
line <- Console.ReadLine()
// Recover the standard output stream so that a
// completion message can be displayed.
let standardOutput = new StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush <- true
Console.SetOut standardOutput
Console.WriteLine $"INSERTTABS has completed the processing of {args[0]}."
0
with :? IOException as e ->
let errorWriter = Console.Error
errorWriter.WriteLine e.Message
errorWriter.WriteLine usageText
1
Imports System.IO
Public Module InsertTabs
Private Const tabSize As Integer = 4
Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"
Public Function Main(args As String()) As Integer
If args.Length < 2 Then
Console.WriteLine(usageText)
Return 1
End If
Try
' Attempt to open output file.
Using writer As New StreamWriter(args(1))
Using reader As New StreamReader(args(0))
' 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(reader)
Dim line As String = Console.ReadLine()
While line IsNot Nothing
Dim newLine As String = line.Replace("".PadRight(tabSize, " "c), ControlChars.Tab)
Console.WriteLine(newLine)
line = Console.ReadLine()
End While
End Using
End Using
Catch e As IOException
Dim errorWriter As TextWriter = Console.Error
errorWriter.WriteLine(e.Message)
errorWriter.WriteLine(usageText)
Return 1
End Try
' Recover the standard output stream so that a
' completion message can be displayed.
Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush = True
Console.SetOut(standardOutput)
Console.WriteLine($"INSERTTABS has completed the processing of {args(0)}.")
Return 0
End Function
End Module
Uwagi
Ta metoda może służyć do ponownego uzyskiwania standardowego strumienia wyjściowego po jego zmianie przez metodę SetOut .
Zobacz też
Dotyczy
OpenStandardOutput(Int32)
Uzyskuje standardowy strumień wyjściowy, który jest ustawiony na określony rozmiar buforu.
public:
static System::IO::Stream ^ OpenStandardOutput(int bufferSize);
public static System.IO.Stream OpenStandardOutput (int bufferSize);
static member OpenStandardOutput : int -> System.IO.Stream
Public Shared Function OpenStandardOutput (bufferSize As Integer) As Stream
Parametry
- bufferSize
- Int32
Ten parametr nie ma żadnego wpływu, ale jego wartość musi być większa lub równa zero.
Zwraca
Standardowy strumień wyjściowy.
Wyjątki
bufferSize
jest mniejsze niż lub równe zero.
Przykłady
Poniższy przykład ilustruje użycie OpenStandardOutput metody . Zastępuje cztery następujące po sobie znaki spacji w ciągu zawierającym znak tabulacji. Aby go uruchomić, należy podać dwa argumenty wiersza polecenia. Pierwszy to nazwa istniejącego pliku tekstowego, do którego zostanie przekierowany standardowy strumień danych wejściowych. Drugi to nazwa pliku, do którego zostanie przekierowany standardowy strumień danych wyjściowych. Ten plik nie musi istnieć. Jeśli istnieje, jego zawartość zostanie zastąpiona.
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;
}
using System;
using System.IO;
public class InsertTabs
{
private const int tabSize = 4;
private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
public static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine(usageText);
return 1;
}
try
{
// Attempt to open output file.
using (var writer = new StreamWriter(args[1]))
{
using (var reader = new StreamReader(args[0]))
{
// 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(reader);
string line;
while ((line = Console.ReadLine()) != null)
{
string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
}
}
}
catch(IOException e)
{
TextWriter errorWriter = Console.Error;
errorWriter.WriteLine(e.Message);
errorWriter.WriteLine(usageText);
return 1;
}
// Recover the standard output stream so that a
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine($"INSERTTABS has completed the processing of {args[0]}.");
return 0;
}
}
open System
open System.IO
let tabSize = 4
let usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt"
[<EntryPoint>]
let main args =
if args.Length < 2 then
Console.WriteLine usageText
1
else
try
// Attempt to open output file.
use reader = new StreamReader(args[0])
use writer = new StreamWriter(args[1])
// 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 reader
let mutable line = Console.ReadLine()
while line <> null do
let newLine = line.Replace(("").PadRight(tabSize, ' '), "\t")
Console.WriteLine newLine
line <- Console.ReadLine()
// Recover the standard output stream so that a
// completion message can be displayed.
let standardOutput = new StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush <- true
Console.SetOut standardOutput
Console.WriteLine $"INSERTTABS has completed the processing of {args[0]}."
0
with :? IOException as e ->
let errorWriter = Console.Error
errorWriter.WriteLine e.Message
errorWriter.WriteLine usageText
1
Imports System.IO
Public Module InsertTabs
Private Const tabSize As Integer = 4
Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"
Public Function Main(args As String()) As Integer
If args.Length < 2 Then
Console.WriteLine(usageText)
Return 1
End If
Try
' Attempt to open output file.
Using writer As New StreamWriter(args(1))
Using reader As New StreamReader(args(0))
' 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(reader)
Dim line As String = Console.ReadLine()
While line IsNot Nothing
Dim newLine As String = line.Replace("".PadRight(tabSize, " "c), ControlChars.Tab)
Console.WriteLine(newLine)
line = Console.ReadLine()
End While
End Using
End Using
Catch e As IOException
Dim errorWriter As TextWriter = Console.Error
errorWriter.WriteLine(e.Message)
errorWriter.WriteLine(usageText)
Return 1
End Try
' Recover the standard output stream so that a
' completion message can be displayed.
Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush = True
Console.SetOut(standardOutput)
Console.WriteLine($"INSERTTABS has completed the processing of {args(0)}.")
Return 0
End Function
End Module
Uwagi
Ta metoda może służyć do ponownego uzyskiwania standardowego strumienia wyjściowego po jego zmianie przez metodę SetOut .