帮助解析文件(使用C#)以获取开始和停止数据/时间戳. [英] help with parsing a file (using C#) for a start and stop data/time stamp.

查看:104
本文介绍了帮助解析文件(使用C#)以获取开始和停止数据/时间戳.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是C#的新手.我来自C/C ++世界.我正在一个项目中,我需要解析一个包含数据记录的文本文件.记录的每个新数据都带有时间戳.我需要找到一个特定的日期和时间戳.然后在那一点上,开始逐行读取数据(将其保存到另一个temp.txt文件中),直到我到达结束日期/时间戳为止.然后完成.

示例:
用户输入(通过GUI)的开始时间为2011年1月30日01:20:00.
然后他们输入2011年1月31日星期一1:20:06的结束时间
然后,我将有一个开始按钮",然后该程序将解析整个文件以获取开始日期/时间戳,并开始写入一个新的临时文件,其中包含从此刻起直到结束时间的数据.
这里的要点是用户将不知道确切的开始/结束时间.所以我把它们放在小时:分钟,没有秒.所以我假设我的代码只会搜索字符串
Sun Jan 30 01:07:2011.(注意,未显示秒数)
在"C"世界中,我将创建一个while或for循环.但是我已经在C#世界中阅读了解析文件的更简便方法吗?
当然,我将需要进行一些错误捕获,以便如果输入的日期与文件中的任何字符串都不匹配,那么我会将用户设置为已知.

.....记住我不懂C#,这只是我想出的一个例子.您能给我一个可以满足我需求的代码段吗?
谢谢!

Hello all,

I am fairly new to C#. I come from a C/C++ world. I am working on a project that I need to parse a text file that contains data logging. Each new data that is logged is time stamped. I need to find a specific date and time stamp. Then at that point, start reading the data (saving it off to another temp.txt file), line by line until I come to an end date/time stamp. Then done.

Example:
user inputs (via the GUI) start time of Sun Jan 30 01:20:00 2011.
Then they enter a end time of Mon Jan 31 10:20:06 2011
Then I will have a "go button" that then has the program parse the entire file for the start date/time stamp and begin writing a new temp file with the data from this point on until end time is found.
The catch here is that the user will not know the exact time to start/end. So I am having them put in the hour:min and no seconds. So I would assume my code would search for only the string
Sun Jan 30 01:07: 2011. (note no seconds shown)
In the "C" world I would create a while or for loop. But I have read in the C# world there are easier ways to parse a file?
And of course I would need to put in some error trapping such that if the ented dates do not match any strings in the file then I set the user know.

..... remember I don''t know C#, this is just an example I came up with. Can you give me a snippet that will do what I want?
thanks!

using (StreamReader mystr = new StreamReader("datafile.txt")) 
{
   while (true)
    {
        datalines = mystr.ReadLine();
        if (datalines == startdate)
            break; # found the start point
        else
            
            continue;
    }
  
    
    while ((datalines = mystr.ReadLine()) != endDate)
    {
        #write the line out to a temp file
    }
}
mystr.close



[edit]添加了代码块以保留格式-OriginalGriff [/edit]



[edit]Code block added to preserve formatting - OriginalGriff[/edit]

推荐答案

有一个简单得多的方法:不需要循环:
There is a much simpler way: no loops required:
string startdate = "Sun Jan 30 01:20:00 2011";
string enddate = "Mon Jan 31 10:20:06 2011";
string content = File.ReadAllText(@"F:\Temp\justsometext.txt");
int start = content.IndexOf(startdate);
if (start >= 0)
    {
    start += startdate.Length;
    int end = content.IndexOf(enddate, start);
    if (end >= 0)
        {
        string log = content.Substring(start, end - start);
        Console.WriteLine(log);
        }
    }


[edit] JSOP非常正确地指出了一个错误:已修复-OriginalGriff [/edit]


[edit]JSOP very correctly pointed out a bug: fixed - OriginalGriff[/edit]


您将必须从每个日志条目中检索日期/时间才能执行自己的操作想.我编写了一个技巧/窍门,可以轻松地将两个DateTime对象与您关心的部分进行比较:

部分DateTime对象相等 [
You''re going to have to retrieve the date/time from each log entry to do what you want. I wrote a tip/trick that makes it easy to compare two DateTime objects for just the parts you care about:

Partial DateTime Object Equality[^]


确定. .所以我只是用C#编写了一个小型测试应用程序来解决这个问题.
它有效,但可能并不优雅.就像约翰在上面说的那样,我的终点将不考虑在下一分钟之前的同一分钟内发生的任何行.所以我需要解决这个问题.
这是代码段.希望得到您的反馈.

OK... so I just wrote a small test app in C# to fiddle around with this.
It works, but probably not graceful. and like John said above, my end point would not take into consideration any lines that occur in the same minute before the next minute. So I need to fix that.
here is the snippet. I would appreciate any feedback.

private void button1_Click(object sender, EventArgs e)
        {
            string dataRow = "";
            string startdate = "Feb 5 01:10:";
            string enddate = "Feb 5 04:15:";

            try
            {
                TextReader tread = new StreamReader("acutemp.log");
                TextWriter twrite = new StreamWriter("newtemp.txt");

                while (true)
                {
                    dataRow = tread.ReadLine(); // read each line of the file
                    if (dataRow.Contains(startdate))
                        break;
                    else
                        //
                        continue;
                }
                while ((dataRow = tread.ReadLine()) != null)
                {
                    twrite.WriteLine(dataRow); // write the lines from start to end dates

                    if (dataRow.Contains(enddate)) // if found the end date then exit loop
                        break;
                }


                tread.Close();
                twrite.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



[edit]添加了代码块以保留格式-OriginalGriff [/edit]



[edit]Code block added to preserve formatting - OriginalGriff[/edit]


这篇关于帮助解析文件(使用C#)以获取开始和停止数据/时间戳.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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