Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read very long input from console in C#?

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

...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.

like image 877
Dragoon Aethis Avatar asked Mar 17 '12 14:03

Dragoon Aethis


1 Answers

You have to add following line of code in your main() method:

byte[] inputBuffer = new byte[4096];
                Stream inputStream = Console.OpenStandardInput(inputBuffer.Length);
                Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length));

Then you can use Console.ReadLine(); to read long user input.

like image 77
Manmay Barot Avatar answered Sep 22 '22 18:09

Manmay Barot