The topics in this part are listed below. The code snippets if any are based on the new C++ .NET syntaxes. The code snippets if any are based on the new C++ .NET syntaxes.
Console represents the standard input, output, and error streams for console applications. This class cannot be inherited. The console is an operating system window where users interact with the operating system or a text-based console application by entering text input through the computer keyboard, and reading text output from the computer terminal. For example, in Windows the console is called the command prompt window and accepts MS-DOS commands. The Console class provides basic support for applications that read characters from, and write characters to, the console. For example:
|
Figure 15
This code sample demonstrates how to read from and write to the standard input and output streams. These streams can also be redirected using the SetIn and SetOut methods. Next, try the following code. The following code example illustrates the usage of the ReadLine() method.
#include "stdafx.h"
// Read the metadata for MSCORLIB
// #using <mscorlib.dll> //included by default
using namespace System;
using namespace System::IO;
// Read the content of 'inputfile.txt' and write it to 'outputfile.txt'
int main()
{
// Read from the command line argument
// This program run at command prompt
array<String^>^args = Environment::GetCommandLineArgs();
const int tabSize = 4;
// You can press the TAB key to invoke the executable program name.
// The inputfile.txt and outputfile.txt is in the same project folder
// and there are some text in the inputfile.txt. You need to create it
// before running this program
String^ usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
StreamWriter^ writer = nullptr;
// Check the command line arguments
if (args->Length < 3)
{
Console::WriteLine(usageText);
return 1;
}
try
{
writer = gcnew StreamWriter(args[2]);
Console::SetOut(writer);
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;
}
Output:
Figure 16
You can check the fileoutput.txt content. The following example demonstrates the use of the Console::Read() method.
// This example demonstrates the Console::Read() method.
#include "stdafx.h"
// Read the metadata for MSCORLIB
// #using <mscorlib.dll> //included by default
using namespace System;
int main()
{
String^ m1 = "\nType a string of text then press Enter.\n"
"Type '+' anywhere in the text to quit:\n";
String^ m2 = "Character '{0}' is hexadecimal 0x{1:x4}.";
String^ m3 = "Character is hexadecimal 0x{0:x4}.";
Char ch;
int x;
// Write to the standard output
Console::WriteLine(m1);
do
{
x = Console::Read();
try
{
ch = Convert::ToChar(x);
if ( Char::IsWhiteSpace(ch))
{
Console::WriteLine(m3, x);
if (ch == 0x0a)
Console::WriteLine(m1);
}
else
Console::WriteLine(m2, ch, x);
}
catch (OverflowException^ e)
{
Console::WriteLine("{0} Value read = {1}.", e->Message, x);
ch = Char::MinValue;
Console::WriteLine(m1);
}
}
while ( ch != '+' );
}
Output:
----------------------------------------------------------------
Figure 17