跟踪一个StreamReader的线的位置 [英] Tracking the position of the line of a streamreader

查看:220
本文介绍了跟踪一个StreamReader的线的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要做的是跟踪,我从流中读取读取时我说了线的位置 reader.ReadLine()我需要知道该行的文件,我的位置也希望能够再读取我有previously跟踪的位置文件。

这可能吗?如果是这样,请协助。

帮助是非常AP preciated

先谢谢了。


解决方案

您可以做三种方式这一个:

1)写自己的StreamReader。这里有一个很好的起点:<一href=\"http://stackoverflow.com/questions/829568/how-to-know-positionlinenumber-of-a-streamreader-in-a-textfile\">How要知道在一个文本文件一个StreamReader的位置(行号)?

2)StreamReader类有两个非常重要的,但私有变量称为charPos和charLen所需要的定位实际的读的位置,而不是仅仅在流的基本位置。你可以使用反射来获取值的建议<一href=\"http://www.daniweb.com/software-development/csharp/threads/35078/streamreader-and-position\">here


 的Int32 charpos =(Int32)已s.GetType()。InvokeMember(charPos
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic可|
BindingFlags.Instance | BindingFlags.GetField
 ,空,S,NULL);INT32 charlen =(Int32)已s.GetType()。InvokeMember(charLen
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic可|
BindingFlags.Instance | BindingFlags.GetField
 ,空,S,NULL);回报(Int32)已s.BaseStream.Position-charlen + charpos;


3)简单地读取整个文件转换成字符串数组。事情是这样的:


 的char [] = CRLF字符新[2] {'\\ n','\\ r'};
TR的TextReader = File.OpenText(一些文件路径);
字符串[] = fileLines tr.ReadToEnd()斯普利特(CRLF)。


另一种可能性(沿萨姆斯行作为#3)处于所述行读取和存储线在数组中。当你想阅读之前的线路,只需使用数组。

Hi guys what i need to do is track the position of the line that I am reading from the stream reader when I say reader.ReadLine() i need to know the position of that line in the file and I also want to be able to then read the file from the position i have previously tracked.

Is this possible? If so please assist.

Help is much appreciated

Thanks in advance.

解决方案

You can do this one of three ways:

1) Write your own StreamReader. Here's a good place to start: How to know position(linenumber) of a streamreader in a textfile?

2) The StreamReader class has two very important, but private variables called charPos and charLen that are needed in locating the actual "read" position and not just the underlying position of the stream. You could use reflection to get the values as suggested here

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;

3) Simply read the entire file into a string array. Something like this:

char[] CRLF = new char[2] { '\n', '\r' };
TextReader tr = File.OpenText("some path to file");
string[] fileLines = tr.ReadToEnd().Split(CRLF);

Another possibility (along the sames lines as #3) is to read in the lines and store the line in an array. When you want to read the prior line, just use the array.

这篇关于跟踪一个StreamReader的线的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆