如何阅读在C#中的控制台很长的输入? [英] How to read very long input from console in C#?

查看:245
本文介绍了如何阅读在C#中的控制台很长的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加载从控制台在C#veeeery线长,可达65000个字符。到Console.ReadLine本身具有的254个字符(+2转义序列)的限制,但我可以用这样的:

I need to load veeeery long line from console in C#, up to 65000 chars. Console.ReadLine itself has a limit of 254 chars(+2 for escape sequences), but I can use this:

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);
}



...克服这一限制,多达8190个字符(+2转义序列) - 在VS不幸的是我需要输入WAY大线,当READLINE_BUFFER_SIZE设为什么比8192更大,错误没有足够的存储是可用来处理此命令显示出来缓冲区应设置为65536。我已经试过了几个解决方案,要做到这一点,但我仍然在学习,没有超过或者1022或8190个字符,我怎么能增加限额为65536?先谢谢了。

...to overcome that limit, for up to 8190 chars(+2 for escape sequences) - unfortunately I need to enter WAY bigger line, and when READLINE_BUFFER_SIZE is set to anything bigger than 8192, error "Not enough storage is available to process this command" shows up in VS. Buffer should be set to 65536. I've tried a couple of solutions to do that, yet I'm still learning and none exceeded either 1022 or 8190 chars, how can I increase that limit to 65536? Thanks in advance.

推荐答案

尝试Console.Read用的StringBuilder

try Console.Read with StringBuilder

        StringBuilder sb =new StringBuilder();
        while (true) {
            char ch = Convert.ToChar(Console.Read());
            sb.Append(ch);
            if (ch=='\n') {
                break;
            }
        }

这篇关于如何阅读在C#中的控制台很长的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆