Console.OpenStandardOutput Console.OpenStandardOutput Console.OpenStandardOutput Console.OpenStandardOutput Method

정의

표준 출력 스트림을 가져옵니다.Acquires the standard output stream.

오버로드

OpenStandardOutput() OpenStandardOutput() OpenStandardOutput() OpenStandardOutput()

표준 출력 스트림을 가져옵니다.Acquires the standard output stream.

OpenStandardOutput(Int32) OpenStandardOutput(Int32) OpenStandardOutput(Int32) OpenStandardOutput(Int32)

표준 출력 스트림을 가져와 지정한 버퍼 크기로 설정합니다.Acquires the standard output stream, which is set to a specified buffer size.

OpenStandardOutput() OpenStandardOutput() OpenStandardOutput() OpenStandardOutput()

표준 출력 스트림을 가져옵니다.Acquires the standard output stream.

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

반환

표준 출력 스트림입니다.The standard output stream.

예제

다음 예제에서는 OpenStandardOutput 메서드를 사용하는 방법을 보여 줍니다.The following example illustrates the use of the OpenStandardOutput method. 탭 문자가 있는 문자열에서 4개의 연속된 공백 문자를 대체합니다.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;
}

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;
    }
}
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

설명

변경 된 후에 표준 출력 스트림에 다시 가져오는 데 사용할 수 있습니다이 메서드는 SetOut 메서드.This method can be used to reacquire the standard output stream after it has been changed by the SetOut method.

추가 정보

OpenStandardOutput(Int32) OpenStandardOutput(Int32) OpenStandardOutput(Int32) OpenStandardOutput(Int32)

표준 출력 스트림을 가져와 지정한 버퍼 크기로 설정합니다.Acquires the standard output stream, which is set to a specified buffer size.

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

매개 변수

bufferSize
Int32 Int32 Int32 Int32

내부 스트림 버퍼 크기입니다.The internal stream buffer size.

반환

표준 출력 스트림입니다.The standard output stream.

예외

bufferSize가 0보다 작거나 같습니다.bufferSize is less than or equal to zero.

예제

다음 예제에서는 OpenStandardOutput 메서드를 사용하는 방법을 보여 줍니다.The following example illustrates the use of the OpenStandardOutput method. 탭 문자가 있는 문자열에서 4개의 연속된 공백 문자를 대체합니다.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;
}

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;
    }
}
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

설명

변경 된 후에 표준 출력 스트림에 다시 가져오는 데 사용할 수 있습니다이 메서드는 SetOut 메서드.This method can be used to reacquire the standard output stream after it has been changed by the SetOut method.

추가 정보

적용 대상