6

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.

Dragoon Aethis
  • 169
  • 3
  • 11
  • 1
    Can't you dump the input to a temp file and simply pass the file name in the console ? – digEmAll Mar 17 '12 at 14:44
  • I can't type more than 8190 chars, it simply won't allow me to enter anymore. I can save the data into the file, yet I need a way to get huge input. – Dragoon Aethis Mar 17 '12 at 14:48
  • 1
    What is your scenario? I'm not really sure that you are trying to solve the right problem – Onkelborg Mar 17 '12 at 14:50
  • It sounds like console isn't your best choice of input. Like dig|EmAll said, you should consider alternative methods of input? Worst case, you could make an app that looks like the console that has large buffers...? – Smudge202 Mar 17 '12 at 14:51
  • @DragoonPL1: What I was trying to say is that it makes little sense to read a huge string from the console. IMO is more reasonable to manage the possibility to pass a name file in the console, e.g. if in the console you receive something in the format `-f `, you read the file content. – digEmAll Mar 17 '12 at 14:55
  • 2
    You must have a seriously hyperactive cat to test this kind of code. – Hans Passant Mar 17 '12 at 15:07
  • I'd seriously want to use other input, like loading such content from file, but I can't use them, only console, only input through Console.ReadLine and similiar. – Dragoon Aethis Mar 17 '12 at 15:18

3 Answers3

3

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.

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
Manmay Barot
  • 165
  • 1
  • 8
2

try Console.Read with StringBuilder

        StringBuilder sb =new StringBuilder();
        while (true) {
            char ch = Convert.ToChar(Console.Read());
            sb.Append(ch);
            if (ch=='\n') {
                break;
            }
        }
hago
  • 1,700
  • 2
  • 16
  • 18
  • That did work, thanks :) I've tried to use this too, but I've used \r\n, not \n, and it just skipped the break instruction. – Dragoon Aethis Mar 17 '12 at 15:19
  • How exactly did you guys get this to work. I need a console application that can take BigIntegers or just reallyyyy big numbers and I cant seem to get this to work.. I can only get the maximum input to be raised using `Console.SetIn(new StreamReader(Console.OpenStandardInput(8192)));` – Chisx Feb 03 '15 at 20:40
0

I agree with Manmay, that seems to work for me, and I also attempt to keep the default stdin so I can restore it afterwards:

        if (dbModelStrPathname == @"con" ||
            dbModelStrPathname == @"con:")
        {
            var stdin = Console.In;

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

            dbModelStr = Console.In.ReadLine();

            Console.SetIn(stdin);
        }
        else
        {
            dbModelStr = File.ReadAllText(dbModelStrPathname);
        }
DennisVM-D2i
  • 416
  • 3
  • 8