Console.Readline() Max Length

Console.ReadLine() max length?

Without any modifications to the code it will only take a maximum of 256 characters ie; It will allow 254 to be entered and will reserve 2 for CR and LF.

The following method will help to increase the limit:

private static string ReadLine()
{
Stream inputStream = Console.OpenStandardInput(READLINE_BUFFER_SIZE);
byte[] bytes = new byte[READLINE_BUFFER_SIZE];
int outputLength = inputStream.Read(bytes, 0, READLINE_BUFFER_SIZE);
//Console.WriteLine(outputLength);
char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength);
return new string(chars);
}

Why does Console.Readline() have a limit on the length of text it allows?

A quick look at implementation with .NET Reflector gives this:

public static Stream OpenStandardInput()
{
return OpenStandardInput(0x100);
}

public static Stream OpenStandardInput(int bufferSize)
{
...
}

256 is the default value of OpenStandardInput, so I guess it's by design. Note this is only for .NET as the Windows API does not have this limit.

Why can I read more than 254 characters with Console.ReadLine when the docs suggest that I shouldn't be able to?

Paraphrasing from here:

The default cmd console mode is "ENABLE_LINE_INPUT", which means that
when code issues a ::ReadFile call against stdin, ::ReadFile doesn't
return to the caller until it encounters a carriage return. But the
call to ReadFile only has a limited size buffer that was passed to it.
Which means that cmd looks at the buffer size provided to it, and
determines based on that how many characters long the line can be...
if the line were longer, it wouldn't be able to store all the data
into the buffer.

The default buffer size used when opening Console.In is now 4096 (source)

The documentation is open source, and available here if you would like to submit an issue or pull request.

When redirecting StandardOut/StandardIn, this limit does not apply. From here:

The limit is how much memory you pass in.

How can I limit the number of characters for a console input? C#

If you can use Console.Read(), you can loop through until you reach the 200 characters or until an enter key is entered.

StringBuilder sb = new StringBuilder();
int i, count = 0;

while ((i = Console.Read()) != 13) // 13 = enter key (or other breaking condition)
{
if (++count > 200) break;
sb.Append ((char)i);
}

EDIT

Turns out that Console.ReadKey() is preferred to Console.Read().

http://msdn.microsoft.com/en-us/library/471w8d85.aspx

Increase buffer for Console.Readline?

From MSDN something like this ought to work:

Stream inputStream = Console.OpenStandardInput();
byte[] bytes = new byte[1536]; // 1.5kb
int outputLength = inputStream.Read(bytes, 0, 1536);

The you can convert your byte array to a string with something like:

var myStr = System.Text.Encoding.UTF8.GetString(bytes);

Delphi Console Application increase input character limit

AFAIK, you can't make the RTL's Readln() function accept more characters (though internally, it is coded to run a loop that should be able to handle more than 254 characters). It seems by default, when you paste your 300-char test string into the console window, it stops taking characters at 254 even before you press Enter.

But, you can use a different approach - call GetStdHandle(STD_INPUT_HANDLE) and then call ReadFile() on that HANDLE to read however much you want. If you use a buffer that is at least 300 bytes, it will happily accept your 300-char test string:

program Project3;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils, Winapi.Windows;

var
buf : array[0..299] of AnsiChar;
MyTest: AnsiString;//string;
hStdIn: THandle;
dwNumRead: DWORD;
begin
try
//Readln(MyTest);
hStdIn := GetStdHandle(STD_INPUT_HANDLE);
ReadFile(hStdIn, buf, sizeof(buf), dwNumRead, nil);
SetString(MyTest, buf, dwNumRead);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

You can then let the RTL handle that buffering for you, by wrapping the HANDLE in a THandleStream and TStreamReader (the latter lets you specify a buffer size - it defaults to 1024 bytes), eg:

program Project3;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils, Winapi.Windows, System.Classes;

var
MyTest : String;
strm: THandleStream;
reader: TStreamReader;
begin
try
//Readln(MyTest);
strm := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
try
reader := TStreamReader.Create(strm);
try
MyTest := reader.ReadLine;
finally
reader.Free;
end;
finally
strm.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

C#: Large amounts of text cut off when read by program

It's a limit of the Console API, it is 256 characters (254 + CR LF).
You can change this limit with

Console.SetIn(new StreamReader(Console.OpenStandardInput(8192)));

Found on MSDN forum: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/51ad87c5-92a3-4bb3-8385-bf66a48d6953

OR

you can create a new ReadLine method, check this post:
Console.ReadLine() max length?

Java console input length limit

Ok, I resolved the issue.
The solution was to set the bigger buffer size for the console in IntelliJ settings:

Sample Image

VB.NET Console.ReadLine() min length?

To limit the input to 10 characters, while allowing for less that 10 characters to be entered by pressing the Enter key, you can use a loop like this. It checks for the enter key and exits the loop if it's pressed, or the loop will naturally end once 10 characters have been entered.

EDIT - updated per comments

Imports System.Text

Module Module1

Sub Main()
Dim userInput = New StringBuilder()
Dim maxLength = 10
While True
' Read, but don't output character
Dim cki As ConsoleKeyInfo = Console.ReadKey(True)
Select Case cki.Key
Case ConsoleKey.Enter
' Done
Exit While
Case ConsoleKey.Backspace
' Last char deleted
If userInput.Length > 0 Then
userInput.Remove(userInput.Length - 1, 1)
Console.Write(vbBack & " " & vbBack)
End If
Case Else
' Only append if less than max entered and it's a display character
If userInput.Length < maxLength AndAlso Not Char.IsControl(cki.KeyChar) Then
userInput.Append(cki.KeyChar)
Console.Write(cki.KeyChar)
End If
End Select
End While
MsgBox("'" & userInput.ToString() & "'")
End Sub

End Module


Related Topics



Leave a reply



Submit