Console.ReadLine Method
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| An I/O error occurred. |
|
| There is insufficient memory to allocate a buffer for the returned string. |
|
| The number of characters in the next line of characters is greater than Int32.MaxValue. |
A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or the value of the Environment.NewLine property. The returned string does not contain the terminating character(s).
If this method throws OutOfMemoryException, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the TextReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.
To avoid such a situation and produce robust code you should use the Read method and store the read characters in a preallocated buffer.
The following code example illustrates the usage of the ReadLine method.
public class InsertTabs { private const int tabSize = 4; private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt"; public static int Main(string[] args) { StreamWriter writer = null; if (args.Length < 2) { Console.WriteLine(usageText); return 1; } try { writer = new StreamWriter(args[1]); Console.SetOut(writer); Console.SetIn(new StreamReader(args[0])); } catch(IOException e) { TextWriter errorWriter = Console.Error; errorWriter.WriteLine(e.Message); errorWriter.WriteLine(usageText); return 1; } string line; while ((line = Console.ReadLine()) != null) { string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t"); Console.WriteLine(newLine); } writer.Close(); // Recover the standard output stream so that a // completion message can be displayed. StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput()); standardOutput.AutoFlush = true; Console.SetOut(standardOutput); Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]); return 0; } }
public class InsertTabs
{
private static int tabSize = 4;
private static String usageText = "Usage: INSERTTABS inputfile.txt"
+ " outputfile.txt";
public static void main(String[] args)
{
StreamWriter writer = null;
if (args.length < 2) {
Console.WriteLine(usageText);
return ;
}
try {
writer = new StreamWriter(args[1]);
Console.SetOut(writer);
Console.SetIn(new StreamReader(args[0]));
}
catch (IOException e) {
TextWriter errorWriter = Console.get_Error();
errorWriter.WriteLine(e.get_Message());
errorWriter.WriteLine(usageText);
return ;
}
String line;
while (((line = Console.ReadLine()) != null)) {
String newLine = line.Replace("".PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a
// completion message can be displayed.
StreamWriter standardOutput = new StreamWriter(Console.
OpenStandardOutput());
standardOutput.set_AutoFlush(true);
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.",
args[0]);
} //main
} //InsertTabs
const tabSize = 4; const usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt"; var writer : StreamWriter = null; var args = Environment.GetCommandLineArgs(); if (args.Length != 3) { Console.WriteLine(usageText); Environment.Exit(1); } try { writer = new StreamWriter(args[2]); Console.SetOut(writer); Console.SetIn(new StreamReader(args[1])); } catch(e : IOException) { var errorWriter = Console.Error; errorWriter.WriteLine(e.Message); errorWriter.WriteLine(usageText); Environment.Exit(1); } var line; while ((line = Console.ReadLine()) != null) { var newLine = line.Replace(("").PadRight(tabSize, ' '), "\t"); Console.WriteLine(newLine); } writer.Close(); // 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 {0}.", args[0]);
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
{
System.ConsoleKeyInfo keyinfo;
System.String linia="";
System.Int32 pozycja=System.Console.CursorLeft;
System.Console.CursorVisible=true;
while(Kontynuuj)
{
keyinfo=System.Console.ReadKey(true);
if(keyinfo.Key==System.ConsoleKey.Enter)
{
System.Console.SetCursorPosition(0,System.Console.CursorTop+1);
System.Console.CursorVisible=false;
break;
}
else if(keyinfo.Key==System.ConsoleKey.Backspace)
{
if(linia.Length>0)
{
pozycja--;
linia=linia.Substring(0,linia.Length-1);
}
}
else if(keyinfo.KeyChar!='\0')
{
linia+=keyinfo.KeyChar.ToString();
pozycja++;
}
if(keyinfo.KeyChar!='\0')
{
System.Console.SetCursorPosition(0,System.Console.CursorTop);
System.Console.Write(linia+" ");
System.Console.SetCursorPosition(pozycja,System.Console.CursorTop);
}
}
return linia;
}
- 12/20/2007
- PioM