0

I have a line that is about 1.5kb of text. I want my console app to read it but only the first 255 characters can be pasted in. How do I increase this limit? I am literally reading it using Console.ReadLine() in debug mode under visual studio 2013

  • Searching [MSDN](https://msdn.microsoft.com/en-us/library/system.console.readline%28v=vs.110%29.aspx) for `Console.Readline` this comment "To read longer lines, call the OpenStandardInput(Int32) method" should have lead you to [OpenStandardInput](https://msdn.microsoft.com/en-us/library/5kdtbb63(v=vs.110).aspx) – Matt Burland Jan 30 '15 at 21:40
  • 2
    I'll accept that as an answer. I wrote `using (var r = new StreamReader(Console.OpenStandardInput(2048))) { myvar = r.ReadLine(); }` it worked perfectly. I thought the answer would be in a app.config or Console.IncreaseBuffer(size) or something. –  Jan 30 '15 at 21:47
  • I didn't check out the cause but it appears my last comment has problems. It doesn't pick up the full line. –  Jan 31 '15 at 04:00

2 Answers2

3

This have been already discussed a couple times. Let me introduce you to the best solution i saw so far ( Console.ReadLine() max length? )

The concept: verriding the readline function with OpenStandartInput (like the guys in the comments mentioned):

The implementation:

private static string ReadLine()
{
    Stream inputStream = Console.OpenStandardInput(READLINE_BUFFER_SIZE); // declaring a new stream to read data, max readline size
    byte[] bytes = new byte[READLINE_BUFFER_SIZE]; // defining array with the max size
    int outputLength = inputStream.Read(bytes, 0, READLINE_BUFFER_SIZE); //reading
    //Console.WriteLine(outputLength); - just for checking the function
    char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength); // casting it to a string
    return new string(chars); // returning
}

This way you get the most you can from the console, and it'll work for more than 1.5 KB.

Community
  • 1
  • 1
A. Abramov
  • 1,823
  • 17
  • 45
  • ONE problem with this code. It includes the end of line characters so it actually broke my code until i put a `.Trim()` –  Jan 30 '15 at 22:14
  • Ohhh i see. CF/ EF, yeah. Sorry i didn't pay attention to that, good luck with your program (: @acidzombie24 – A. Abramov Jan 30 '15 at 22:15
2

From MSDN something like this ought to work:

Stream inputStream = Console.OpenStandardInput();
byte[] bytes = new byte[1536];    // 1.5kb
int outputLength = inputStream.Read(bytes, 0, 1536);

The you can convert your byte array to a string with something like:

var myStr = System.Text.Encoding.UTF8.GetString(bytes);
Matt Burland
  • 44,552
  • 18
  • 99
  • 171