51

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? enter image description here

Tim Dams
  • 747
  • 1
  • 6
  • 15
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
  • 4
    "after a couple of lines"? ReadLine() should only return 1 line! ;D – Jeffrey L Whitledge Apr 05 '11 at 20:15
  • @Jeffrey: Sorry, I mean in a normal cmd.exe console size, it is a few lines with wrap around :-) – Kyle Brandt Apr 05 '11 at 20:17
  • 1
    @Jeffrey: Added image to show you want I mean, it won't let my type any more 'a' characters then seen in that screenshot. – Kyle Brandt Apr 05 '11 at 20:20
  • /* Console.ReadLine() maximum is 254*/ Console.WriteLine("Length = " + Console.ReadLine().Length); // StreamReader can read in a whole lot more. It can read in several thousand characters in once – user3108703 May 12 '15 at 13:31

10 Answers10

35

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();
ara
  • 466
  • 5
  • 6
  • 1
    There are no words. I love you. – joelc Jan 10 '14 at 18:29
  • 2
    There is absolutely no reason to create an array (inputBuffer) just to use the .Length property. Petr Matas' answer is exactly the same, except that it doesn't use a confusing/unused array. – Wouter Jul 29 '18 at 12:34
  • How do you get a character count higher than 1024? I've tried higher numbers yet 1024 always seems to be the limit. – trigger_segfault Oct 07 '18 at 21:16
34

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);
    }
Kokul Jose
  • 1,384
  • 2
  • 14
  • 26
stack72
  • 8,198
  • 1
  • 31
  • 35
  • 2
    Nice solution. For an alternative, see [this later answer](http://stackoverflow.com/a/6081967/1336654) to a duplicate of the question here on SO. It uses `Console.SetIn` to change the "in" stream. – Jeppe Stig Nielsen Jul 10 '12 at 11:32
  • When i tried to do this with a base64 encoded string, some of it got turned into chinese characters, went for ara's answer – Max Carroll Oct 02 '19 at 10:36
23

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();
Community
  • 1
  • 1
Petr Matas
  • 231
  • 2
  • 2
22

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 !

hoang
  • 1,887
  • 1
  • 24
  • 34
9

Console.ReadLine() has a 254 character limit.

I found the below single line of code here. That seemed to do the trick.

Console.SetIn(new StreamReader(Console.OpenStandardInput(8192)));

AKM
  • 509
  • 9
  • 10
1

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;
    } 
Bala R
  • 107,317
  • 23
  • 199
  • 210
0

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);
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
cartheur
  • 9
  • 2
0

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
Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
0

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.

sarvesh
  • 2,743
  • 1
  • 20
  • 30
-1

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);
    }
}
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162