Advertisement

Managing Console I/O Operations

By Bintu Chaudhary   Posted at  12:52:00 AM   Managing Console I/O Operations

Home  »  C#  »  Managing Console I/O Operations


     Console is the set of devices throught which a user communicates with a system using interactive set of commands. GUI is not considered as console in this respect. The primary input device in most of the computer systems is a keyboard and the primary output is the monitor. The pair forms the user's console in most of the caseds

Contents

The Console Class

     To allow console input/output operations, C# provides, C# provides a console class. The Console class provides basic input and output support for applications that read from and write characters to the console. The standard input, output, and error streams are represented by properties, and are automatically associated with the console when the application starts. Application can redirected these properties to other streams; for example, streams associated with fies instead of the console.

By default, the read methods in console class use the standard input stream (keyboard) and the write methods use the standard output (monitor) stream.

The write methods support writing data with or without automatically appending carriage return and linefeed characters. This enables the writing of string, formatted strings, arrays of characters, instances of primitive types, and arbitrary objects without first having to convert them to strings.

The following example demonstrates the use of basic Console input and output functions.



Output



Managing Console I/O Operations

     Console input/ouput operations are carried out by the Console class which is inside system namespace and is stored in Mscorlib (in Mscorlib.dll) assembly.

Data from the console is read from the standard input stream; normal data to the console is written to the standard output stream; and error data to the console is written to the standard error output stream. These streams are automatically associated with the console when your application start, and are presented to you as the In, Out, and Error properties.

By default, the value of the In property is a System.IO.TextReader, while the values of the Out and Error properties are System.IO.TextWriter objects. However, you can associate these properties with streams that do not represent the console. This class synchronized TextReader and TextWriter instances. Multiple threads can concurrently read from or write to an instance of this type.

Console.OpenStandardInput Method()

     This method acquires the standard input stream. The code given below shows this. This method can be used to reacquire the standard input stream after it has been changed by the SetIn method. Consider the following code snippet for illustration.
The following code sample illustrates the user of OpenStandardInput:

public class Decoder
{
  public static void Main()
  {
    Stream inputStream = Console.OpenStandardInput();
    byte [] bytes = new byte[100];
    Console.WriteLine("To decode, type or paste the UTF7 encoded string and press enter:");
    Console.WriteLine("(Example: \"M+APw-nchen ist wundervoll\")");
    int outputLength = inputStream.Read(bytes, 0 ,100);
    char [] chars = Encoding.UTF7.GetChars(bytes, 0 , outputLenght);
    Console.WriteLine("Decoded String:");
    Console.WriteLine(new string(chars));
  }
}


Console.OpenStandardOutput Method()

     The method acquires the standard output stream. This method can be used to reqcquire the standard output stream after it has been changed by the SetOut method. Consider the following code snippet for illustration.

Output








Console.Read Method()

     This method reads the next character from the standard input stream. It returns the next character from the input stream, or negative one (-1) if no more characters are available. This method will not return until the read operation is terminated; for example by the user pressing the enter key. If data is available, the input stream contains what the user entered, suffixed with the environment dependent newline character. Consider the following for illustration.

int i;
char c;
while(true)
{
  i = Console.Read();
  if (i == 1)
  break;
  c = (char) l;
  Console.WriteLIne("Echo: )0)",c);
}
Console.WriteLine("Done");
return 0;


Console.ReadLine Method()

     This method reads the next line of characters from the standard input stream. It returns the next line from the input stream, or a null reference if no more charcters are available. A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a). The returned string does not contain the termination character(s). Consider the following code for illustration.

Output







Console.Write Method(Boolean)

     This method writes the text representation of the specified Boolean value to the standard output stream. The text representation of value is produced by calling Boolean.Tostring. Consdier the following code for illustration.




Console.Write Method(String)

     This method writes the specified string value to the standard output stream. If value is a null reference, nothing is written to the standard output stream. Consider the following code for illustration.








About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me