get count of ReadLine in StreamReader

  • Thread starter Thread starter somequestion
  • Start date Start date
S

somequestion

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?
 
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
 
Hi,

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?

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()
i mean
when i using this
while ((str = sr.ReadLine()) != null)
{
// 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
}
}
 
somequestion said:
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?

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()
i mean
when i using this
while ((str = sr.ReadLine()) != null)
{
// wanna read next Line of sr.ReadLine()
}
is it possible?

That's what "str" is - it's the next line from the reader.
 
Morten said:
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.
 
Back
Top