Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

get count of ReadLine in StreamReader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
somequestion - 22 Nov 2006 01:39 GMT
Question 1.
i am using StreamReader Class like this...
     string str = string.Empty;
     StreamReader  sr = new StreamReader(fsIn,Encoding.Default)
     while ((str = sr.ReadLine()) != null)
     {
             // wanna get line Count of Sr
       }

i just want to get a count of sr.
how can i get this...?  isn`t there any method in StreamReader Calss?

Question 2.
is there any way to get data of next data of sr.ReadLine()
i mean
when i using this
while ((str = sr.ReadLine()) != null)
{
   // wanna read next Line of sr.ReadLine()              
}
is it possible?
Kevien Lee - 22 Nov 2006 02:13 GMT
i just want to get a count of sr.
// I have an idea that you can get the StreamReader  as bit
,as the 'char' is 4 bit lenght,when you cout the StreamReader's bit
number,
it would be get you want.however,this just an idea,i not sure that it
would be ok

> Question 1.
> i am using StreamReader Class like this...
[quoted text clipped - 17 lines]
>  }
> is it possible?
Morten Wennevik - 22 Nov 2006 07:34 GMT
Hi,

> Question 1.
> i am using StreamReader Class like this...
[quoted text clipped - 7 lines]
> i just want to get a count of sr.
> how can i get this...?  isn`t there any method in StreamReader Calss?

You can't get the number of lines in the file using a StreamReader.  You  
will either have to read it all and increment a counter or use  
File.ReadAllLines().

> Question 2.
> is there any way to get data of next data of sr.ReadLine()
[quoted text clipped - 4 lines]
>     // wanna read next Line of sr.ReadLine()                }
> is it possible?

No, there is no PeekLine functionality in the StreamReader, and due to  
buffering, you can't set the position of the stream to create your own  
PeekLine either.  You can, however, just read the next line, and add some  
functionality to handle two lines instead of just one.

using (StreamReader sr = new StreamReader(fStream, FileMode.Open)))
{
    string line = string.Empty;
    string next = sr.ReadLine();
   
    while ((line = next) != null)
    {
        next = sr.ReadLine();
        // process line, and peeking at next
    }
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]

semkaa@gmail.com - 23 Nov 2006 11:48 GMT
> No, there is no PeekLine functionality in the StreamReader, and due to
> buffering, you can't set the position of the stream to create your own
> PeekLine either.  You can, however, just read the next line, and add some
> functionality to handle two lines instead of just one.

You can change offset using this:
StreamReader.BaseStream.Seek(indexStart, SeekOrigin.Begin);

I have the same problem. I have to count and then parse lines in ascii
file with about 30 000 000 records. It takes very long time to count
lines in single thread just incrementing counter. So I do it using
System.Threading. First you need to split file by parts, and then, for
each part, open
separate thread and increment static counter using
Interlocked.Increment(ref int). Works more faster if you have
multiprocess machine.
Jon Skeet [C# MVP] - 22 Nov 2006 20:16 GMT
> Question 1.
> i am using StreamReader Class like this...
[quoted text clipped - 7 lines]
> i just want to get a count of sr.
> how can i get this...?  isn`t there any method in StreamReader Calss?

No, there isn't - but it's extremely straightforward. Just increment a
counter until you've read all the lines.

> Question 2.
> is there any way to get data of next data of sr.ReadLine()
[quoted text clipped - 5 lines]
>  }
> is it possible?

That's what "str" is - it's the next line from the reader.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.