C# 读取文本文件中的附加信息

C# 读取文本文件中的附加信息,c#,C#,我有一个附加了所有信息的文本文件,我想把这些信息读到一个列表中。这是我的文本文件的设计 ------->26/05/2015 17:15:52<------------------ Index :0-0 Index :1-0 Index :2-20150527 Index :3-182431 ------->27/05/2015 17:15:52<------------------ Index :0-0 Index :1-0 Index :2-20150527 Inde

我有一个附加了所有信息的文本文件,我想把这些信息读到一个列表中。这是我的文本文件的设计

------->26/05/2015 17:15:52<------------------
Index :0-0
Index :1-0
Index :2-20150527
Index :3-182431
------->27/05/2015 17:15:52<------------------
Index :0-0
Index :1-0
Index :2-20150527
Index :3-182431
------->28/05/2015 17:15:52<------------------
Index :0-0
Index :1-0
Index :2-20150527
Index :3-182431

--->26/05/2015 17:15:5227/05/2015 17:15:5228/05/2015 17:15:52您将希望使用这样的代码来解析文件

//load the whole file in to memory
var lines = File.ReadAllLines("path-to-file"); //don't forget to add using System.IO;

//you will have to fill in your specific logic
MyCustomObject currentObject = null;
List<MyCustomObject> objects = new List<MyCustomObject>();

//loop over the lines in the file
foreach(var line in lines) {
    if(line.StartsWith("------->")) {
        //header line

        //Again, fill in your logic here
        currentObject = new MyCustomObject();
        currentObject.SetHeader(line);
        objects.Add(currentObject);
    } else {
        //body line

        //double check that the file isn't malformed
        if(currentObject == null) throw new Exception("Missing header record at beginning of file!");

        //process the line
        currentObject.AddLine(line);
    }
}
//done!
//将整个文件加载到内存中
var lines=File.ReadAllLines(“文件路径”)//不要忘记添加using System.IO;
//你必须填写你的具体逻辑
MyCustomObject currentObject=null;
列表对象=新列表();
//在文件中的行上循环
foreach(行中的var行){
if(第行开始使用(“----->”){
//标题行
//同样,在这里填写您的逻辑
currentObject=新的MyCustomObject();
currentObject.SetHeader(行);
添加(currentObject);
}否则{
//身体线条
//仔细检查文件是否存在格式错误
如果(currentObject==null)抛出新异常(“文件开头缺少头记录!”);
//处理生产线
currentObject.AddLine(行);
}
}
//完成了!

首先,我们应该定义“新”一词,如果它的意思是:

  • 在以前的迭代中尚未阅读
  • 文件中的新节
假设您指的是文件中的新节,则可以定义表示项的类:

class Item
{
    public List<string> Indexes;
    public string Header;

    public Item()
    {
        Indexes= new List<string>();
    }
}
类项目
{
公共列表索引;
公共字符串头;
公共项目()
{
索引=新列表();
}
}
并使用如下简单循环解析文件:

    List<Item> items = new List<Item>();

    var lines = File.ReadAllLines("path-to-file");
    Item currentItem = null;
    foreach (var line in lines)
    {
        if (line.StartsWith("------->"))
        {
            if (currentItem != null)
            {
                items.Add(currentItem);
            }
            currentItem=new Item();
            currentItem.Header = line;
        }
        else if (currentItem != null)
        {
            currentItem.Indexes.Add(line);
        }
    }
    if (currentItem!=null)
        items.Add(currentItem);
List items=newlist();
var lines=File.ReadAllLines(“文件路径”);
Item currentItem=null;
foreach(行中的var行)
{
if(第行开始使用(“----->”)
{
如果(currentItem!=null)
{
项目。添加(当前项目);
}
currentItem=新项();
currentItem.Header=行;
}
else if(currentItem!=null)
{
currentItem.Indexes.Add(行);
}
}
如果(currentItem!=null)
项目。添加(当前项目);
如果您的意思是“到目前为止尚未读取”,那么您可能还应该在“Item”类中存储输入日期,并将读取输入日期与集合中已经存在的日期进行比较,并且只读取新的日期


你也应该考虑文件是否被清除(旋转),然后你必须决定是否读取整个文件是有意义的,或者你应该只从一些没有读过的行读取,使用一些变量来存储在上一次迭代中读取的行数。还有其他类似的事情。

行以
-->
开头的事实应该告诉您这是一个新项目。我猜
-->
显示您在给定时间添加了一个新项目?或者,这只是一个占位符,附加了一些内容,而不是文件的内容?我认为您应该将到目前为止读取的行数存储在某个位置。当你继续监视文件时,只需读取新行。这是新项目的信息,但当我从文件中读取信息时,我怎么能用c语言说我正在阅读新项目?非常感谢,这就是我需要的:)