none
關於StreamWriter寫入txt檔案 RRS feed

  • 問題

  • 以下是我的程式碼

    System.Diagnostics.Stopwatch clock = new System.Diagnostics.Stopwatch();

    StreamWriter WriteFile = new StreamWriter("Muti.txt", true);
                int min;
                //==============================
                for (int pp = 0; pp < T1able.Count; pp++)
                {
                    for (int ii = 0; ii < Table2.Count; ii++)
                    {
                        for (int cc = 0; cc < CT3able.Count; cc++)
                        {
                            for (int mm = 0; mm < Table4.Count; mm++)
                            {
                                PopulationCount = Table1[pp];
                                iteraCount = Table2[ii];
                                CrossoverRate = Table3[cc];
                                MutationRate = Table4[mm];
                                WriteFile.WriteLine("====================================================");
                                WriteFile.WriteLine("PopulationCount : " + PopulationCount);
                                WriteFile.WriteLine("iteraCount : " + iteraCount);
                                WriteFile.WriteLine("CrossoverRate : " + CrossoverRate);
                                WriteFile.WriteLine("MutationRate : " + MutationRate);
                                clock.Reset();//碼表歸零
                                clock.Start();//碼表開始計時
                                for (int i = 0; i < SimulationTime; i++)
                                {
                                    GaInitial();
                                    Initial();
                                    GenerateInitialPopulation();
                                    for (int j = 0; j < iteraCount; j++)
                                    {
                                        CountFitness(BusDistance, CarDistance);
                                        min = Reproduction();
                                        fitnesslog[j][0] = min;
                                    }
                                    WriteFile.WriteLine(fitnesslog[iteraCount - 1][0].ToString());
                                }
                                clock.Stop();//碼錶停止
                                WriteFile.WriteLine("計算時間=" + (clock.Elapsed.TotalMilliseconds / 1000).ToString());
                            }
                        }
                    }
                }
                    //==============================
                WriteFile.Close(); 

     

    請問一下,為何StreamWriter 寫入文件,並不會即時寫入,都是一大段一大段的分次寫入。

    電腦有時會熱當,希望起碼能記錄到熱當前的數據,但是常常txt文件是空文件。

    2011年10月4日 上午 04:31

解答

  • 你可以設定 StreamWriter的AutoFlush屬性為true試一下!

     


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
    • 已編輯 亂馬客 2011年10月4日 上午 04:45
    • 已標示為解答 熊掌 2011年10月4日 上午 05:17
    2011年10月4日 上午 04:45

所有回覆