942,510 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 20941
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 19th, 2006
0

Re: StreamReader and Position

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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jan 19th, 2006
0

Re: StreamReader and Position

if i get time i will have a look more closely
Anyone know how to get 26hrs out of a day yet?
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 19th, 2006
0

Re: StreamReader and Position

Sure, if you're willing to experiment with personal quasiperiodicty.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jan 30th, 2006
0

Re: StreamReader and Position

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mfm24 is offline Offline
1 posts
since Jan 2006
May 12th, 2006
0

Re: StreamReader and Position

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jake1164 is offline Offline
2 posts
since May 2006
Mar 22nd, 2007
0

Re: StreamReader and Position

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joepgrooten is offline Offline
1 posts
since Mar 2007
Nov 4th, 2009
0

Streamreader override

Click to Expand / Collapse  Quote originally posted by r0ckbaer ...
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)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Thilahar84 is offline Offline
1 posts
since Nov 2009
Apr 2nd, 2010
0
Re: StreamReader and Position
Hi this is my solution. I tried to use all of presented, but in each I found some not useful for me sides or bags. I don't wont to say that my solution is useful, I just modified your suggestions.

c# Syntax (Toggle Plain Text)
  1. /// <summary>
  2. /// Class can be used only for text files.
  3. /// Theoretically class can process any encoding, but was checked only for ANSI and UTF8
  4. /// If you want to use this class with any other encoding, please check it.
  5. /// </summary>
  6. public class PositionableStreamReader : StreamReader
  7. {
  8. private long _position;
  9.  
  10. public PositionableStreamReader(string fileName, Encoding enc ):base(fileName, enc)
  11. {
  12. //we need to add length of symbol which is in begin of file and describes encoding of file
  13. if(IsPreamble())
  14. {
  15. _position = this.CurrentEncoding.GetPreamble().Length;
  16. }
  17. }
  18.  
  19.  
  20. /// <summary>
  21. /// Encoding can really haven't preamble
  22. /// </summary>
  23. public bool IsPreamble()
  24. {
  25. byte[] preamble = this.CurrentEncoding.GetPreamble();
  26. bool res = true;
  27. for(int i = 0; i < preamble.Length; i++)
  28. {
  29. int dd = base.BaseStream.ReadByte();
  30. if (preamble[i] != dd)
  31. {
  32. res = false;
  33. break;
  34. }
  35. }
  36. Position = 0;
  37. return res;
  38. }
  39.  
  40. /// <summary>
  41. /// Use this property for get and set real position in file.
  42. /// Position in BaseStream can be not right.
  43. /// </summary>
  44. public long Position
  45. {
  46. get { return _position; }
  47. set
  48. {
  49. ((Stream)base.BaseStream).Seek(value, SeekOrigin.Begin);
  50. this.DiscardBufferedData();
  51.  
  52. }
  53. }
  54.  
  55. public override string ReadLine()
  56. {
  57. string line = base.ReadLine();
  58. if (line != null)
  59. {
  60. _position += CurrentEncoding.GetByteCount(line);
  61. }
  62. _position += CurrentEncoding.GetByteCount(Environment.NewLine);
  63. return line;
  64. }
  65. }
Last edited by hanklabor; Apr 2nd, 2010 at 1:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hanklabor is offline Offline
1 posts
since Apr 2010
Dec 14th, 2011
0
Re: StreamReader and Position
Borrowing from Matt's reflection code, here are some extension methods that get the job done:
c# Syntax (Toggle Plain Text)
  1. using System.IO;
  2. using System.Reflection;
  3.  
  4. /// <summary>
  5. /// Contains extension methods for this namespace.
  6. /// </summary>
  7. public static class ExtensionMethods
  8. {
  9. /// <summary>
  10. /// Gets the current read position of the StreamReader.
  11. /// </summary>
  12. /// <param name="streamReader">The StreamReader object to get the position for.</param>
  13. /// <returns>Current read position in the StreamReader.</returns>
  14. public static int GetPosition(this StreamReader streamReader)
  15. {
  16. // Based on code shared on www.daniweb.com by user mfm24(Matt).
  17. int charpos = (int) streamReader.GetType().InvokeMember(
  18. "charPos",
  19. BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
  20. null,
  21. streamReader,
  22. null);
  23. int charlen= (int) streamReader.GetType().InvokeMember(
  24. "charLen",
  25. BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
  26. null,
  27. streamReader,
  28. null);
  29. return (int)streamReader.BaseStream.Position - charlen + charpos;
  30. }
  31.  
  32. /// <summary>
  33. /// Sets the current read position of the StreamReader.
  34. /// </summary>
  35. /// <param name="streamReader">The StreamReader object to get the position for.</param>
  36. /// <param name="position">The position to move to in the file, starting from the beginning.</param>
  37. public static void SetPosition(this StreamReader streamReader, long position)
  38. {
  39. streamReader.BaseStream.Seek(position, SeekOrigin.Begin);
  40. streamReader.DiscardBufferedData();
  41. }
  42. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hsummerhays is offline Offline
2 posts
since Dec 2011
Dec 18th, 2011
0
Re: StreamReader and Position
Borrowing from Matt's reflection code, here are some extension methods that get the job done:
c# Syntax (Toggle Plain Text)
  1. using System.IO;
  2. using System.Reflection;
  3.  
  4. /// <summary>
  5. /// Contains extension methods for this namespace.
  6. /// </summary>
  7. public static class ExtensionMethods
  8. {
  9. /// <summary>
  10. /// Gets the current read position of the StreamReader.
  11. /// </summary>
  12. /// <param name="streamReader">The StreamReader object to get the position for.</param>
  13. /// <returns>Current read position in the StreamReader.</returns>
  14. public static int GetPosition(this StreamReader streamReader)
  15. {
  16. // Based on code shared on www.daniweb.com by user mfm24(Matt).
  17. int charpos = (int) streamReader.GetType().InvokeMember(
  18. "charPos",
  19. BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
  20. null,
  21. streamReader,
  22. null);
  23. int charlen= (int) streamReader.GetType().InvokeMember(
  24. "charLen",
  25. BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
  26. null,
  27. streamReader,
  28. null);
  29. return (int)streamReader.BaseStream.Position - charlen + charpos;
  30. }
  31.  
  32. /// <summary>
  33. /// Sets the current read position of the StreamReader.
  34. /// </summary>
  35. /// <param name="streamReader">The StreamReader object to get the position for.</param>
  36. /// <param name="position">The position to move to in the file, starting from the beginning.</param>
  37. public static void SetPosition(this StreamReader streamReader, long position)
  38. {
  39. streamReader.BaseStream.Seek(position, SeekOrigin.Begin);
  40. streamReader.DiscardBufferedData();
  41. }
  42. }
Unfortunately, when I try to use this code, it all builds but raises an exception at run-time:

System.FieldAccessException was unhandled
Message=System.IO.StreamReader.charPos
StackTrace:
at System.RuntimeType.InternalInvokeMember(Type thisType, String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters, StackCrawlMark& stackMark)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
at ExtensionMethods.GetPosition(StreamReader streamReader)

Is there something I've done wrong to call it, or does it mean that in my environment (Windows Phone development), this method won't work?

Regards

Philip
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pcolmer is offline Offline
1 posts
since Dec 2011
Message:
Previous Thread in C# Forum Timeline: C# Console App to DLL?
Next Thread in C# Forum Timeline: Browsing first/next/previous in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC