StreamReader problem
-
Hi, In code below I convert string to stream and now I have problem. The value of stremReader.Readline() is never null. What I do wrong?
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(stringToPrint); MemoryStream stream = new MemoryStream(bytes); StreamReader strReader = new StreamReader(stream); while (currentLine < linesPerPage) { lineText = strReader.ReadLine(); if (lineText == null) // PROBLEM - never null break; .... more code
thanks -
Hi, In code below I convert string to stream and now I have problem. The value of stremReader.Readline() is never null. What I do wrong?
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(stringToPrint); MemoryStream stream = new MemoryStream(bytes); StreamReader strReader = new StreamReader(stream); while (currentLine < linesPerPage) { lineText = strReader.ReadLine(); if (lineText == null) // PROBLEM - never null break; .... more code
thanksI would suggest using the StreamReader[^] class:
StringReader sr = new StringReader(stringToPrint); ....
-
Hi, In code below I convert string to stream and now I have problem. The value of stremReader.Readline() is never null. What I do wrong?
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(stringToPrint); MemoryStream stream = new MemoryStream(bytes); StreamReader strReader = new StreamReader(stream); while (currentLine < linesPerPage) { lineText = strReader.ReadLine(); if (lineText == null) // PROBLEM - never null break; .... more code
thanks -
try a loop like this :
Do While strReader.Peek() >= 0
Console.WriteLine(strReader.ReadLine())
Loop -
I would suggest using the StreamReader[^] class:
StringReader sr = new StringReader(stringToPrint); ....
-
I think you mean in stringToPrint is path to file, but it isn't. In stringToPrint is text converted from StringBuilder variable and contains more lines. I just solve problem with the suggest from second reply. Thanks for your effort.
I was just providing a suggestion as to prevent needless overhead for reading the string. Grats on solving the problem.