| | |
StreamReader and Position
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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.
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.
•
•
Join Date: Jan 2006
Posts: 1
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: May 2006
Posts: 2
Reputation:
Solved Threads: 0
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:
Here is a link to the VB.NET post with code:
http://www.daniweb.com/techtalkforum...tml#post214443
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
•
•
Join Date: Mar 2007
Posts: 1
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Hi, what about a derived class like this one (it's very basic, but it might be what you are searching for):
C# Syntax (Toggle Plain Text)
public class MyStream : StreamReader { private uint _pos; public MyStream(string path, System.Text.Encoding enc) : base(path, enc) { } public override string ReadLine() { char current; int i; string line = null; while ((i = base.Read()) != -1) { current = (char)i; _pos++; if (IsFeed(current)) { if ((i = base.Peek()) != -1) { if (!IsFeed((char)i)) break; } } else line += current; } return line; } private bool IsFeed(char c) { if (c == '\r' || c == '\n') return true; return false; } public uint Pos { get { return _pos; } } }
Thank u for ur this code. I'm using in my product.
THanks & regards,
CRT.
![]() |
Similar Threads
- StreamReader and Position (In VB) (VB.NET)
Other Threads in the C# Forum
- Previous Thread: Listview item display
- Next Thread: Product names showing as the class name
| Thread Tools | Search this Thread |
.net access ado.net algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime decryption degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest i18n image imageprocessing index input install java label list listbox listener mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update uploadatextfile usercontrol users validation view visualstudio webbrowser whileloop windows winforms wpf xml







