从文件中读取一行后如何记录位置? [英] How to file position after reading one line from a file?
本文介绍了从文件中读取一行后如何记录位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中如何读取一行并在阅读完如何获取文件位置后?
In C# how to read a line and after reading how to get the file position?
如何实现两个?
FileStream fStream = File.OpenRead(filePath);
var reader = new StreamReader(fStream);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
}
如果我调用fStream.Position读取一行,它会将位置值作为整个文件长度只有。
After reading a line if I call fStream.Position , it gives position value as entire file length only.
请帮我解决我的问题。
推荐答案
你可以更好地使用你的BaseStream属性读者获取您当前的位置。并且因为FileStream作为StreamReader应用IDisposable,所以最好将整个包含在using子句中:
You could better use the BaseStream property of your reader to get your current position. And because either FileStream as StreamReader apply IDisposable you should better enclose the whole in a using clause:
using (StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open)))
{
while ((line = reader.ReadLine()) != null)
{
Debug.WriteLine(reader.BaseStream.Position);
//do sth. with line.
}
}
wizend
wizend
这篇关于从文件中读取一行后如何记录位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文