Checking for newline in a stream - StreamReader.ReadLine() is totally useless 
Author Message
 Checking for newline in a stream - StreamReader.ReadLine() is totally useless

Try this code:

using System;
using System.IO;

class MemoryBuffer
{
        static void Main(string[] args)
        {
                MemoryStream ms = new MemoryStream();
                StreamWriter sw = new StreamWriter(ms);
                StreamReader sr = new StreamReader(ms);

                sw.AutoFlush = true;

                sw.Write("No newlines for now...");

                ms.Position = 0;

                string s = sr.ReadLine();

                if(s == null)
                        Console.WriteLine("No newline available");
                else
                        Console.WriteLine("Newline available - text:
{0}",s);

                sw.WriteLine(" ...but here come one");

                ms.Position = 0;

                s = sr.ReadLine();

                if(s == null)
                        Console.WriteLine("No newline available");
                else
                        Console.WriteLine("Newline available - text:
{0}",s);

                s = sr.ReadLine();

                if(s == null)
                        Console.WriteLine("No newline available");
                else
                        Console.WriteLine("Newline available - text:
{0}",s);

                Console.ReadLine();

                return;
        }

Quote:
}

You'll see that in the first case ReadLine() returns all the text in the
stream, but no newline is present.
Documentation says it should return null if reaching EOS, but says nothing
about what happens if newlines are not there. Actually, it returns null only
if there is *absolutely nothing* in the stream.

So, how can I reliably check for a newline in a stream without having to
read every single char ?

Massimo



Sat, 25 Jun 2005 12:50:26 GMT  
 Checking for newline in a stream - StreamReader.ReadLine() is totally useless
Massimo,

    You are using the wrong tool for the job.  You should be reading
character by character using the Read method, and then check for the new
line or carriage return/line feed combination yourself if you need to know
when you come across these characters in this situation.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> Try this code:

> using System;
> using System.IO;

> class MemoryBuffer
> {
>         static void Main(string[] args)
>         {
>                 MemoryStream ms = new MemoryStream();
>                 StreamWriter sw = new StreamWriter(ms);
>                 StreamReader sr = new StreamReader(ms);

>                 sw.AutoFlush = true;

>                 sw.Write("No newlines for now...");

>                 ms.Position = 0;

>                 string s = sr.ReadLine();

>                 if(s == null)
>                         Console.WriteLine("No newline available");
>                 else
>                         Console.WriteLine("Newline available - text:
> {0}",s);

>                 sw.WriteLine(" ...but here come one");

>                 ms.Position = 0;

>                 s = sr.ReadLine();

>                 if(s == null)
>                         Console.WriteLine("No newline available");
>                 else
>                         Console.WriteLine("Newline available - text:
> {0}",s);

>                 s = sr.ReadLine();

>                 if(s == null)
>                         Console.WriteLine("No newline available");
>                 else
>                         Console.WriteLine("Newline available - text:
> {0}",s);

>                 Console.ReadLine();

>                 return;
>         }
> }

> You'll see that in the first case ReadLine() returns all the text in the
> stream, but no newline is present.
> Documentation says it should return null if reaching EOS, but says nothing
> about what happens if newlines are not there. Actually, it returns null
only
> if there is *absolutely nothing* in the stream.

> So, how can I reliably check for a newline in a stream without having to
> read every single char ?

> Massimo



Sun, 26 Jun 2005 01:24:03 GMT  
 Checking for newline in a stream - StreamReader.ReadLine() is totally useless



Quote:
>     You are using the wrong tool for the job.  You should be reading
> character by character using the Read method, and then check for the new
> line or carriage return/line feed combination yourself if you need to know
> when you come across these characters in this situation.

So, there's absolutely no way to read single lines of text without scanning
single characters ? :-(

Massimo



Sun, 26 Jun 2005 05:45:46 GMT  
 Checking for newline in a stream - StreamReader.ReadLine() is totally useless

Quote:


> >     You are using the wrong tool for the job.  You should be reading
> > character by character using the Read method, and then check for the new
> > line or carriage return/line feed combination yourself if you need to know
> > when you come across these characters in this situation.

> So, there's absolutely no way to read single lines of text without scanning
> single characters ? :-(

Not without blocking, as far as I can see.

One thing you should be aware of is that you should get the Decoder for
the appropriate Encoding object. That way you can call GetChars on it,
which maintains state if you pass it an array of bytes which doesn't
actually finish at the end of a character, which may well happen
whenever you're using multibyte characters and receiving the data over
the network.

--
Jon Skeet

If replying to the group, please do not mail me at the same time



Sun, 26 Jun 2005 16:39:44 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. StreamReader.ReadLine method hangs

2. problem using StreamReader.ReadLine

3. how i can limite the Stream.Readline command?

4. I am totally lost today?

5. I am totally lost today?

6. StreamReader To Stream

7. How do I check for a newline character ?

8. Checking Streams

9. Checking pending input in a stream

10. how to check for invalid file stream

11. first time error return on checking file stream validity

12. Checking Who Am I?

 

 
Powered by phpBB® Forum Software