解析一个值的文件然后返回顶部 [英] Parse file for one value then return to top

查看:72
本文介绍了解析一个值的文件然后返回顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码解析各个字段的文件



I am parsing a file for various fields using the following code

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
    string line = sr.ReadLine();
    int startIndex, endIndex = -1;

    while (line != null)
    {
        if (line.IndexOf("value") > -1)
        {
            startIndex = line.IndexOf("value") + 5;

............
............



而不是解析我需要的所有字段,我想解析一个特定的值,如果该值存在并且它通常位于文件末尾的某个地方,那么我希望返回到文件的顶部并解析为其余的价值观。否则,如果这个值不存在,我不想浪费时间解析所有值。



我的问题是如何返回到顶部文件并重新开始解析,如果我的价值在那里?





谢谢


Instead of parsing for all the fields I need I would like to parse for one specific value, if that value exists and its normally somewhere at the end of the file I would then like to return to the top of the file and parse for the rest of the values. Otherwise I don't want to waste my time parsing for all the values if this one value does not exist.

My problem is how do I return to the top of the file and start parsing all over again if my found value is there?


Thanks

推荐答案

使用 BufferedStream [ ^ ],它允许你寻找文件中的不同位置,包括开头。
Use a BufferedStream[^], which allows you to seek to different positions within the file, including the beginning.


我发现它后我最终使用了它。



I ended up using this once I found it.

stream.DiscardBufferedData();
stream.BaseStream.Seek(0, SeekOrigin.Begin);


这篇关于解析一个值的文件然后返回顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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