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

时间:2014-11-10 16:16:35

标签: c# csv

正如标题所示,我有一个程序在完成将其输出写入文件之前结束。我不确定这是C#的限制还是什么。

以下是相关代码:

    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.
            }
        }
    }

该程序旨在从文本文件(ASCII或Unicode不可知)中提取课程目录信息,按照特定格式对其进行解析,然后将其分成以CSV格式注入另一个文本文件的字段。然后,此CSV格式的信息将合并到MSSQL数据库中。

然而,在我看来,源文件中的信息越长,程序在删除最后几行文本时遇到的问题就越多。我该怎么做才能纠正这个问题?我去年根据旧信息运行程序,它似乎运行得很好。

2 个答案:

答案 0 :(得分:2)

使用后始终关闭流。

最后添加:

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

更好的方法是将它们放在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)
       {
          ......
       }
    }
  }
}

答案 1 :(得分:0)

在while循环之外添加以下内容

dest.Flush();

或将Auto flush设置为true /关闭流。有关详细信息,请参阅here