Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将特定行保存到新文本文件_C# - Fatal编程技术网

C# 将特定行保存到新文本文件

C# 将特定行保存到新文本文件,c#,C#,我正忙于处理结构如下所示的文本文件: 这是一个连续字符串中的降雨数据,日期后每5个字符表示一个月中的一天 0005880 W 1926 9-7777-7777-7777-7777-7777-7777-7777-7777-7777 117 130 64-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 0005880 W 192610-7777-777

我正忙于处理结构如下所示的文本文件:
这是一个连续字符串中的降雨数据,日期后每5个字符表示一个月中的一天

0005880 W 1926 9-7777-7777-7777-7777-7777-7777-7777-7777-7777  117  130   64-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777
0005880 W 192610-7777-7777-7777-7777-7777-7777-7777-7777-7777   23-7777-7777-7777-7777    3-7777  226  462   71-7777-7777  157   76   15-7777-7777-7777-7777-7777-7777-7777
0005880 W 192611    3   20-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777   61  142-7777-7777-7777    8-7777-7777-7777-7777
0005880 W 192612-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777  132-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777
年和月用字符串中的(10,4)和(14,2)位置表示。 我的问题是,有时下一行不是接下来的月份。我已经编写了代码,添加了一行,其中缺少一个月的数据,如下所示

public void findGapsToolStripMenuItem_Click(object sender, EventArgs e)
    {


        TabPage tp = new TabPage();
        RichTextBox rtb = new RichTextBox();
        rtb.Dock = DockStyle.Fill;
        rtb.Multiline = true;
        rtb.AcceptsTab = true;
        rtb.WordWrap = false;





        Stream myStream;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {


                tp.Controls.Add(rtb);
                tabControl1.TabPages.Add(tp);


                string strfilename = openFileDialog1.FileName;
                string[] lines = File.ReadAllLines(strfilename);
                string[] pathArr = strfilename.Split('\\');
                string[] fileArr = pathArr.Last().Split();
                string filen = fileArr.Last().ToString();



                tp.Text = filen;

                int pyear = 0;
                int pmon = 0;
                int imon = 0;
                int iyear = 0;


                foreach (string line in lines)
                {
                    string missing = "-9999";
                    string year = line.Substring(10, 4);
                    string mon = line.Substring(14, 2);
                    iyear = Convert.ToInt32(year);
                    imon = Convert.ToInt32(mon);


                    if (pyear == 0)
                    {
                        pyear = iyear;
                        pmon = imon;

                        rtb.AppendText(line + "\n");

                    }
                    else
                    {
                        int pt = pyear * 12 + pmon;
                        int t = iyear * 12 + imon;
                        if ((pt + 1) == t)
                        {

                            rtb.AppendText(line + "\n");
                        }
                        else
                        {

                            rtb.AppendText("Missing Months =" + (t - pt) + "\n");

                        }

                        if (line.Contains(missing))
                        {
                            rtb.AppendText("Missing Days" + "\n");


                        }


                        pyear = iyear;
                        pmon = imon;
                    }

                    rtb.SelectAll();
                    rtb.SelectionAlignment = HorizontalAlignment.Left;
                    rtb.SelectionFont = new Font("Consolas", 10);








                }

            }

        }


    }
我的问题是,有没有一种方法可以将缺少的月份或日期之前的所有行导出到名为“开始日期”的文本文件中,并将其导出到缺少的月份或日期之前的日期。例如
1926.9.1926.10.txt
。然后,在下一个丢失的月份或日期之前,继续浏览文件以获取下一部分数据。因此,基本上是以多个文本文档结束的,其中包含数年或数月的数据,没有任何空白。我还希望它能自动创建一个带有站点号的文件夹,该编号为前14个字符(即0005880 W),所有文本文件都将在其中创建

更新

 public void findGapsToolStripMenuItem_Click(object sender, EventArgs e)
    {


        TabPage tp = new TabPage();
        RichTextBox rtb = new RichTextBox();
        rtb.Dock = DockStyle.Fill;
        rtb.Multiline = true;
        rtb.AcceptsTab = true;
        rtb.WordWrap = false;





        Stream myStream;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {


                tp.Controls.Add(rtb);
                tabControl1.TabPages.Add(tp);


                string strfilename = openFileDialog1.FileName;
                string[] lines = File.ReadAllLines(strfilename);
                string[] pathArr = strfilename.Split('\\');
                string[] fileArr = pathArr.Last().Split();
                string filen = fileArr.Last().ToString();
                string pat = @"C:\Test\" + filen;
                System.IO.Directory.CreateDirectory(pat);
                int i;





                tp.Text = filen;

                int pyear = 0;
                int pmon = 0;
                int imon = 0;
                int iyear = 0;
                int j = 1;


                foreach (string line in lines)
                {
                    using (StreamWriter sw = new StreamWriter(@"C:\Test\" + filen+".txt"))
                    {



                        string missing = "-9999";
                        string year = line.Substring(10, 4);
                        string mon = line.Substring(14, 2);
                        iyear = Convert.ToInt32(year);
                        imon = Convert.ToInt32(mon);
                        string filepath = @"C:\Test\" + year + "." + mon+".txt";

                        if (pyear == 0)
                        {

                            File.CreateText(filepath);
                            pyear = iyear;
                            pmon = imon;

                            rtb.AppendText(line + "\n");

                            sw.WriteLine(line);


                        }
                        else
                        {

                            File.CreateText(filepath);
                            int pt = pyear * 12 + pmon;
                            int t = iyear * 12 + imon;
                            if ((pt + 1) == t)
                            {

                                rtb.AppendText(line + "\n");
                                sw.WriteLine(line);


                            }
                            else
                            {

                                string path = pat + "\\" + year + "." + mon + ".txt";
                                File.CreateText(path);

                                rtb.AppendText("Missing Months =" + (t - pt) + "\n");

                            }

                            if (line.Contains(missing))
                            {

                                string path = pat + "\\" + year + "." + mon + ".txt";
                                File.CreateText(path);
                                rtb.AppendText("Missing Days" + "\n");


                            }


                            pyear = iyear;
                            pmon = imon;


                        }

                        rtb.SelectAll();
                        rtb.SelectionAlignment = HorizontalAlignment.Left;
                        rtb.SelectionFont = new Font("Consolas", 10);




                    }
                }







            }


        }
    }

您可以使用System.IO.File类的各种方法创建文件:

此类包括用于创建文件以及将任意文本行写入文件的方法

您可以使用System.IO.Directory类的方法创建目录:

更新:这里有一些伪代码

startdate = null
foreach(line in the input file)
{
    currentdate = date on this line in the input file

    if(startdate == null)
    {
        // We are at the start of a new block of dates
        startdate = currentdate        
        add this line to a list (in memory)
    }
    else if(currentdate == lastdate in the list + 1 month)
    {
        // This date is consecutive
        add this line to a list (in memory)
    }
    else
    {
        // We have a gap in the data
        write out all data in the list to file named <startdate>-<lastdate in list>
        startdate = currentdate
        add this line to the list (which we've just emptied)
    }
}
write out the last file
startdate=null
foreach(输入文件中的行)
{
currentdate=输入文件中此行的日期
if(startdate==null)
{
//我们正处在一段新的日期的开始
开始日期=当前日期
将此行添加到列表(内存中)
}
else if(currentdate==列表中的最后日期+1个月)
{
//这个日期是连续的
将此行添加到列表(内存中)
}
其他的
{
//我们在数据上有差距
将列表中的所有数据写入名为-
开始日期=当前日期
将这一行添加到列表中(我们刚刚清空)
}
}
写出最后一个文件

这只是一个非常粗略的过程,但应该指出编写此代码所需的思考方式。需要明确的一点是,如果要使用日期块的结束日期命名文件,则在找到该块中的最后一行之前,无法创建文件,因此需要将这些行存储在内存中,直到找到日期中的间隔,或输入文件的结尾。

您可以使用System.IO.file类的各种方法创建文件:

此类包括用于创建文件以及将任意文本行写入文件的方法

您可以使用System.IO.Directory类的方法创建目录:

更新:这里有一些伪代码

startdate = null
foreach(line in the input file)
{
    currentdate = date on this line in the input file

    if(startdate == null)
    {
        // We are at the start of a new block of dates
        startdate = currentdate        
        add this line to a list (in memory)
    }
    else if(currentdate == lastdate in the list + 1 month)
    {
        // This date is consecutive
        add this line to a list (in memory)
    }
    else
    {
        // We have a gap in the data
        write out all data in the list to file named <startdate>-<lastdate in list>
        startdate = currentdate
        add this line to the list (which we've just emptied)
    }
}
write out the last file
startdate=null
foreach(输入文件中的行)
{
currentdate=输入文件中此行的日期
if(startdate==null)
{
//我们正处在一段新的日期的开始
开始日期=当前日期
将此行添加到列表(内存中)
}
else if(currentdate==列表中的最后日期+1个月)
{
//这个日期是连续的
将此行添加到列表(内存中)
}
其他的
{
//我们在数据上有差距
将列表中的所有数据写入名为-
开始日期=当前日期
将这一行添加到列表中(我们刚刚清空)
}
}
写出最后一个文件

这只是一个非常粗略的过程,但应该指出编写此代码所需的思考方式。有一点需要明确,如果您想使用日期块的结束日期命名文件,则在找到该块中的最后一行之前,您无法创建该文件,因此您需要将这些行存储在内存中,直到您找到日期中的间隔或输入文件的结束。

很抱歉,我仍然很困惑,没有查看这些链接。你能给我举一个所需代码的例子吗?我还是一个编程新手,因为我是一名水质硕士的学生,认为数据管理程序可以加快原始数据处理速度,所以编程非常新手。您好。链接文档中有很多示例。具体来说,我将查看目录文档中的“CreateDirectory(string)”方法以及文件文档中的“CreateFile('string')”和“AppendText(string)”方法。这些方法正是您所需要的。他们创建一个目录,创建一个文件,并向该文件添加文本。如果它们不清楚或者有什么东西让人困惑,那么你能更具体一点你被困在哪里吗?嗨,我已经将它们合并到我的代码中了,但出于某种原因,它将每个文本文件都写为一行。我想新的文本文件有所有的行,直到它发现一个月失踪,然后创建一个新的文件。我将添加我现在拥有的代码。再次感谢您的帮助。您正在为源文件中的每一行调用File.CreateText。我认为现在的问题在于思考需要实现的算法。我将发布一些伪代码,让你开始。很抱歉,我仍然很困惑,我没有看过那些链接。你能给我举一个所需代码的例子吗?我还是一个编程新手,因为我是一名水质硕士的学生,认为数据管理程序可以加快原始数据处理速度,所以编程非常新手。您好。链接文档中有很多示例。具体来说,我将查看目录文档中的“CreateDirectory(string)”方法以及文件文档中的“CreateFile('string')”和“AppendText(string)”方法。这些方法正是您所需要的。他们创建一个目录,创建一个文件,并向该文件添加文本。如果他们不清楚或者有什么东西让人困惑,那么你能更具体一点你被困在哪里吗