当运行一小段C#代码时,当我试图在Console.ReadLine()中输入一个长字符串时,它似乎在几行之后就被切断了。
Console.Readline()有最大长度限制吗?如果有,有没有办法增加长度?

发布于 2011-04-06 04:15:24
在不对代码进行任何修改的情况下,它将只需要最多256个字符,即;它将允许输入254个字符,并为CR和LF保留2个字符。
以下方法将有助于提高限制:
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);
}发布于 2011-11-08 17:11:01
Stack72的答案的一个问题是,如果在批处理脚本中使用代码,则输入不再是行缓冲的。我在averagecoder.net上找到了一个保留ReadLine调用的替代版本。注意,StreamReader还必须有一个长度参数,因为它也有一个固定的缓冲区。
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();发布于 2013-05-20 02:23:59
这是ara's answer的一个简化版本,它适用于我。
int bufSize = 1024;
Stream inStream = Console.OpenStandardInput(bufSize);
Console.SetIn(new StreamReader(inStream, Console.InputEncoding, false, bufSize));
string line = Console.ReadLine();https://stackoverflow.com/questions/5557889
复制相似问题