When running a small piece of C# code, when I try to input a long string into Console.ReadLine()
it seems to cut off after a couple of lines.
Is there a max length to Console.Readline(), if so is there a way to increase that?
When running a small piece of C# code, when I try to input a long string into Console.ReadLine()
it seems to cut off after a couple of lines.
Is there a max length to Console.Readline(), if so is there a way to increase that?
An issue with stack72's answer is that if the code is used in a batch-script the input is no longer line buffered. I found an alternative version at averagecoder.net that keeps the ReadLine call. Note that StreamReader also must have a length argument, since it has a fixed buffer as well.
byte[] inputBuffer = new byte[1024];
Stream inputStream = Console.OpenStandardInput(inputBuffer.Length);
Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length));
string strInput = Console.ReadLine();
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);
}
This is a simplified version of ara's answer and it works for me.
int bufSize = 1024;
Stream inStream = Console.OpenStandardInput(bufSize);
Console.SetIn(new StreamReader(inStream, Console.InputEncoding, false, bufSize));
string line = Console.ReadLine();
This is a simplified version of Petr Matas' answer. Basically you can specify the buffer size only once as follow :
Console.SetIn(new StreamReader(Console.OpenStandardInput(),
Console.InputEncoding,
false,
bufferSize: 1024));
string line = Console.ReadLine();
Because in the end
Console.OpenStandardInput(int bufferSize)
calls
private static Stream GetStandardFile(int stdHandleName, FileAccess access, int bufferSize)
which doesn't use bufferSize !
ReadLine() internally reads character by character until a -1 or '\n' or '\r\n' is encountered.
public virtual String ReadLine()
{
StringBuilder sb = new StringBuilder();
while (true) {
int ch = Read();
if (ch == -1) break;
if (ch == '\r' || ch == '\n')
{
if (ch == '\r' && Peek() == '\n') Read();
return sb.ToString();
}
sb.Append((char)ch);
}
if (sb.Length > 0) return sb.ToString();
return null;
}
If it is a matter of seeing the entire output of the text in the Console, I have found that the following code works to display it:
Console.SetBufferSize(128, 1024);
This appears to be a limitation of the Windows console. You should try putting your input into a file, and then piping the file into the application. I am not certain if that will work, but it has a chance.
regex_text.exe < my_test_data.txt
Depending on your OS, the command line input will only accept 8191 characters for XP and 2047 characters for NT and Windows 2000. I suggest you pass a filename instead which has your long input and read that file.
Save the input into text and use StreamReader.
using System;
using System.IO;
static void Main(string[] args)
{
try
{
StreamReader sr = new StreamReader("C:\\Test\\temp.txt");
Console.WriteLine(sr.ReadLine().Length);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}