StreamReader and Position

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: StreamReader and Position

 
0
  #11
Jan 19th, 2006
Ok, I understood all that, and really already knew it... but just wanted to double-check.

The reason I can't just override ReadLine(), is because ReadLine() uses internal, private, methods. To do what I need, I would have to re-write ReadLine(). Ok, no problem. But to do that, means I've have to write my own ReadBuffer(), etc.

I think I already travelled down the roads your pointing at, met with dead-ends, and so ended up where I'm at.

And where I'm at works perfectly for my needs... but take a closer look, and if there's a way to derive from StreamReader, with overridden methods, I'd like to see it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: StreamReader and Position

 
0
  #12
Jan 19th, 2006
if i get time i will have a look more closely
Anyone know how to get 26hrs out of a day yet?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: StreamReader and Position

 
0
  #13
Jan 19th, 2006
Sure, if you're willing to experiment with personal quasiperiodicty.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 1
Reputation: mfm24 is an unknown quantity at this point 
Solved Threads: 0
mfm24 mfm24 is offline Offline
Newbie Poster

Re: StreamReader and Position

 
0
  #14
Jan 30th, 2006
I was having a similar problem and noticed there's a very useful StreamReader field called charPos which appears to be what you want. It gives you the position within the buffer. It shows up in the debug window but unfortunately it's private. The only way I could get actually get the value was to use reflection:
Int32 GetCharpos(StreamReader s)
{

Int32 charpos= (Int32) s.GetType().InvokeMember("charPos",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetField
,null, s, null);
Int32 charlen= (Int32) s.GetType().InvokeMember("charLen",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetField
,null, s, null);
return (Int32)s.BaseStream.Position-charlen+charpos;
}

Of course, there may be a very good reason why this field is private, but it appears to work for me. So far. I'm not sure if this can be abused to set the position too.

-Matt
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 2
Reputation: Jake1164 is an unknown quantity at this point 
Solved Threads: 0
Jake1164 Jake1164 is offline Offline
Newbie Poster

Re: StreamReader and Position

 
0
  #15
May 12th, 2006
I just wanted to give a quick thanks!

I have implemented this in VB.NET and it made a HUGE differance over the only other solution I had found:

Dim TR As IO.TextReader = System.IO.File.OpenText(file)
Dim MyFileLine As String = Split(TR.ReadToEnd(), vbCrLf)(lineNumber - 1)

Here is a link to the VB.NET post with code:
http://www.daniweb.com/techtalkforum...tml#post214443
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1
Reputation: joepgrooten is an unknown quantity at this point 
Solved Threads: 0
joepgrooten joepgrooten is offline Offline
Newbie Poster

Re: StreamReader and Position

 
0
  #16
Mar 22nd, 2007
Thanks for your insight!

I was having similair problems, and your adapted version of the StreamReader provided me with enough information to solve it.

Please note that if you want to use Seek() in the StreamReader you must change the value of ReadBytes accordingly ! Otherwise it contains a wrong value after the Seek() operation.


Joep Grooten
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: Thilahar84 is an unknown quantity at this point 
Solved Threads: 0
Thilahar84 Thilahar84 is offline Offline
Newbie Poster

Streamreader override

 
0
  #17
32 Days Ago
Originally Posted by r0ckbaer View Post
Hi, what about a derived class like this one (it's very basic, but it might be what you are searching for):
  1. public class MyStream : StreamReader
  2. {
  3. private uint _pos;
  4.  
  5. public MyStream(string path, System.Text.Encoding enc) : base(path, enc)
  6. {
  7. }
  8.  
  9. public override string ReadLine()
  10. {
  11. char current;
  12. int i;
  13. string line = null;
  14.  
  15. while ((i = base.Read()) != -1)
  16. {
  17. current = (char)i;
  18. _pos++;
  19.  
  20. if (IsFeed(current))
  21. {
  22. if ((i = base.Peek()) != -1)
  23. {
  24. if (!IsFeed((char)i))
  25. break;
  26. }
  27. }
  28. else line += current;
  29. }
  30.  
  31. return line;
  32. }
  33.  
  34. private bool IsFeed(char c)
  35. {
  36. if (c == '\r' || c == '\n')
  37. return true;
  38.  
  39. return false;
  40. }
  41.  
  42. public uint Pos
  43. {
  44. get
  45. {
  46. return _pos;
  47. }
  48. }
  49. }

Thank u for ur this code. I'm using in my product.

THanks & regards,
CRT.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC