简体   繁体   English

程序在输出完成写入文件之前结束

[英]Program ending before output is finished writing to file

As the title suggests, I have a program that ends before it has finished writing its output to a file. 顾名思义,我有一个程序在完成将其输出写入文件之前结束。 I'm not sure if this is a limitation of C# or what. 我不确定这是C#的限制还是什么。

Here is the code in question: 这是有问题的代码:

    static void Main(string[] args)
    {
        StreamReader source = new StreamReader(filepathsource);
        StreamWriter dest = new StreamWriter(filepathdest,false);

        dest.WriteLine("Department,Number,Long Title,Description");

        string line;
        bool dept = false;
        bool num = false;
        bool title = false;
        string temp;

        while ((line = source.ReadLine()) != null)
        {
            dept = false;
            num = false;
            title = false;
            temp = "";

            line = line.TrimStart(' ', '\t');
            if (line.Length > 0)
            {
                foreach (char z in line)
                {
                    if (!dept)
                    {
                        if (z == ' ') //---a space will mark the end of the department code.
                        {
                            dept = true;
                            temp += ",\""; //make a comma and a double quote to ensure the number that follows is a string
                        }
                        else
                            temp += z;
                    }
                    else if (!num)
                    {
                        if (z == '.') //---a period will mark the end of the course number
                        {
                            num = true;
                            temp += "\",\""; //close the string, make a comma, and start a new string
                        }
                        else
                            temp += z;
                    }
                    else if (!title) 
                    {
                        if (z == '.') //---a period will mark the end of the course title
                        {
                            title = true;
                            temp += "\",\""; //close the string, make a comma, and start a new string
                        }
                        else
                            temp += z;
                    }
                    else if(z == '"') //---We are in the Description now and if we find a double quote
                    {
                        temp += "''";  //replace it with two single quotes
                    }
                    else
                        temp += z;
                }
                temp += "\""; //---end the last string
                dest.WriteLine(temp); //---write the comma delimited line to the output file.
            }
        }
    }

The program is designed to pull course catalog information from a text file (ASCII or Unicode agnostic), parse it as it follows a specific format, and then separate it out into fields that are injected into another text file as in CSV format. 该程序旨在从文本文件(与ASCII或Unicode不相关)中提取课程目录信息,按照特定格式对其进行解析,然后将其分离为字段,并以CSV格式注入到另一个文本文件中。 This CSV formatted info would then be merged into a MSSQL database. 然后将此CSV格式的信息合并到MSSQL数据库中。

It seems to me however that the longer the information in the source file, the more issues the program has with cutting out the last few lines of text. 但是在我看来,源文件中的信息越长,程序在切出最后几行文本时遇到的问题就越多。 What can I do to correct this? 我该怎么做才能纠正这个问题? I ran the program last year on old information and it seemed to run just fine. 我去年在旧信息上运行了该程序,它似乎运行得很好。

Always close streams after you use them. 使用流后,请务必关闭它们。

At the end add: 最后添加:

dest.Close();
source.Close();

A better approach is also to put them in a using statement: 更好的方法是将它们放在using语句中:

static void Main(string[] args)
{
  using(StreamReader source = new StreamReader(filepathsource))
  {
     using(StreamWriter dest = new StreamWriter(filepathdest,false))
     {
       dest.WriteLine("Department,Number,Long Title,Description");

       string line;
       bool dept = false;
       bool num = false;
       bool title = false;
       string temp;

       while ((line = source.ReadLine()) != null)
       {
          ......
       }
    }
  }
}

Add the following outside of your while loop 在while循环外添加以下内容

dest.Flush();

or set Auto flush to true / close the stream. 或将“自动刷新”设置为true /关闭流。 See here for more info. 有关更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM