3

我的程序没有遍历文件。程序正确地将所有文件名写入“文件列表”。然后我希望它输出每个文件中数据列表的平均值、最小值和最大值。但是,无论何时运行,它只输出一个文件的平均值、最小值和最大值。我认为循环不是很好。

我尝试在 textwriter tw3 及其右括号之间创建额外的循环,但这不起作用。我也用 tw4 尝试过同样的方法,但它再次不起作用。我不确定问题是由于循环还是我没有使用正确的语法来调用文件夹中的每个文件。整个代码如下。

namespace Filereader_m_15062012
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[] fileEntries;

        private void Form1_Load(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog(); // Show the dialog.
            // create a list to insert the data into
            //put all the files in the root directory into array
            string[] fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.csv");

            // Display all files.
            TextWriter tw1 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/listoffiles.txt");

            List<string> filenames = new List<string>();
            tw1.WriteLine("--- Files: ---");
            foreach (string path in fileEntries)
            {
                tw1.WriteLine(path);
            }

            tw1.Close();

            TextWriter tw2 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/errorlist.txt");
            foreach (string path in fileEntries)
            {  
                    string text = "";

                    // create a list to insert the data into
                    List<float> noise = new List<float>();

                    TextWriter tw3 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/rawdata.txt");

                    string file = path;

                    FileInfo src = new FileInfo(file);
                    TextReader reader = src.OpenText();
                    text = reader.ReadLine();

                    // while the text being read in from reader.Readline() is not null
                    while (text != null)
                    {
                        text = reader.ReadLine();

                        {
                            while (text != null)
                            {
                                text = reader.ReadLine();
                                if (text != null)
                                {
                                    string[] words = text.Split(',');
                                    noise.Add(Convert.ToSingle(words[3]));

                                    // write text to a file
                                    tw3.WriteLine(text);
                                    //foreach (string word in words)
                                    //{
                                    //    tw.WriteLine(word);
                                    //}
                                }

                            }
                        }

                        tw3.Close();


                        int count = 0;
                        float sum = 0;
                        float mean = 0;
                        float max = 0;
                        float min = 100;
                        List<string> means = new List<string>();
                        List<string> maximums = new List<string>();
                        List<string> minimums = new List<string>();

                        TextWriter tw4 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/noise.txt");

                            foreach (float ns in noise)
                            {
                                tw4.WriteLine(Convert.ToString(ns));
                                count++;
                                sum += ns;
                                mean = sum / count;

                                float min1 = 0;


                                if (ns > max)
                                    max = ns;

                                else if (ns < max)
                                    min1 = ns;

                                if (min1 < min && min1 > 0)
                                    min = min1;
                                else
                                    min = min;
                            }

                            means.Add(Convert.ToString(mean));
                            maximums.Add(Convert.ToString(max));
                            minimums.Add(Convert.ToString(min));


                            TextWriter tw5 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymeans.txt");
                            tw5.WriteLine("Mean Noise");
                            tw5.WriteLine("==========");
                            foreach (string m in means)
                            {
                                tw5.WriteLine("mote_noise: {0}", Convert.ToString(m));
                            }

                            tw5.Close();

                            TextWriter tw6 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymaximums.txt");
                            tw6.WriteLine("Maximum Noise");
                            tw6.WriteLine("=============");
                            foreach (string m in maximums)
                            {
                                tw6.WriteLine("mote_noise: {0}", Convert.ToString(m));
                            }

                            tw6.Close();

                            TextWriter tw7 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summaryminimums.txt");
                            tw7.WriteLine("Minimum Noise");
                            tw7.WriteLine("=============");
                            foreach (string m in maximums)
                            {
                                tw7.WriteLine("mote_noise: {0}", Convert.ToString(m));
                            }

                            tw7.Close();


                        tw4.Close();


                    }


                tw2.Close();
            }
        }        


        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

    }
}

我将非常感谢任何人的帮助!!

4

3 回答 3

1

您在迭代文件的 for 循环中初始化您的数字(平均值、模式、总和、计数等) ,以便为每个文件重置它们。

在循环外初始化它们,它应该可以工作。(正如其他人指出的那样,代码还存在其他问题,看起来不是很干净,但这是根本问题。)

于 2012-06-20T13:39:37.080 回答
1

正如 Reniuz 指出的那样,当您遇到此类问题时,您应该单步执行您的代码。在 Visual Studio 中,就像在要开始单步执行的位置设置断点 (F9) 并在调试器到达该点后反复按 F11 一样困难。

但是,在您的情况下,这两个错误来自您的 while 循环和 foreach 循环。循环的正确版本以及完整代码如下所示:

While循环应该是:

// while the text being read in from reader.Readline() is not null
while (text != null)
{
     string[] words = text.Split(',');
     noise.Add(Convert.ToSingle(words[3]));
     // write text to a file
     tw3.WriteLine(text);
     text = reader.ReadLine();
 }

第二个循环应该是:

foreach (float ns in noise)
{
     tw4.WriteLine(Convert.ToString(ns));
     count++;
     sum += ns;
     mean = sum / count;
     float min1 = 0;
     if (ns > max)
     max = ns;
     else if (ns < max)
         min1 = ns;
     if (min1 < min && min1 > 0)
         min = min1;
     means.Add(Convert.ToString(mean));
     maximums.Add(Convert.ToString(max));
     minimums.Add(Convert.ToString(min));
 }

这是完整的代码

  private void Form1_Load(object sender, EventArgs e)
              DialogResult result = folderBrowserDialog1.ShowDialog(); // Show the dialog.
        // create a list to insert the data into
        //put all the files in the root directory into array
        string[] fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.csv");

        // Display all files.
        TextWriter tw1 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/listoffiles.txt");

        List<string> filenames = new List<string>();
        tw1.WriteLine("--- Files: ---");
        foreach (string path in fileEntries)
        {
            tw1.WriteLine(path);
        }

        tw1.Close();

        TextWriter tw2 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/errorlist.txt");
        foreach (string path in fileEntries)
        {
            string text = "";

            // create a list to insert the data into
            List<float> noise = new List<float>();

            TextWriter tw3 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/rawdata.txt");

            string file = path;

            FileInfo src = new FileInfo(file);
            TextReader reader = src.OpenText();
            text = reader.ReadLine();

            // while the text being read in from reader.Readline() is not null
            while (text != null)
            {
                string[] words = text.Split(',');
                noise.Add(Convert.ToSingle(words[3]));

                // write text to a file
                tw3.WriteLine(text);
                text = reader.ReadLine();
            }

            tw3.Close();

            int count = 0;
            float sum = 0;
            float mean = 0;
            float max = 0;
            float min = 100;
            List<string> means = new List<string>();
            List<string> maximums = new List<string>();
            List<string> minimums = new List<string>();

            TextWriter tw4 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/noise.txt");

            foreach (float ns in noise)
            {
                tw4.WriteLine(Convert.ToString(ns));
                count++;
                sum += ns;
                mean = sum / count;
                float min1 = 0;
                if (ns > max)
                    max = ns;

                else if (ns < max)
                    min1 = ns;

                if (min1 < min && min1 > 0)
                    min = min1;
                means.Add(Convert.ToString(mean));
                maximums.Add(Convert.ToString(max));
                minimums.Add(Convert.ToString(min));
            }

            TextWriter tw5 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymeans.txt");
            tw5.WriteLine("Mean Noise");
            tw5.WriteLine("==========");
            foreach (string m in means)
            {
                tw5.WriteLine("mote_noise: {0}", Convert.ToString(m));
            }

            tw5.Close();

            TextWriter tw6 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymaximums.txt");
            tw6.WriteLine("Maximum Noise");
            tw6.WriteLine("=============");
            foreach (string m in maximums)
            {
                tw6.WriteLine("mote_noise: {0}", Convert.ToString(m));
            }
            tw6.Close();

            TextWriter tw7 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summaryminimums.txt");
            tw7.WriteLine("Minimum Noise");
            tw7.WriteLine("=============");
            foreach (string m in maximums)
            {
                tw7.WriteLine("mote_noise: {0}", Convert.ToString(m));
            }
            tw7.Close();
            tw4.Close();
        }


        tw2.Close();
 }
于 2012-06-20T19:53:53.223 回答
1

就像你说的,你的循环对我来说看起来不正确。你为什么不试试这样的东西(“...”代表你的代码部分,为了简洁我没有复制)?请注意,有一个while (text != null)text = reader.ReadLine(); 最后,而不是您示例中的多个 while 和 ifs。

        foreach (string path in fileEntries)
        {
            ...
            string text = reader.ReadLine();

            // while the text being read in from reader.Readline() is not null
            while (text != null)
            {
                string[] words = text.Split(',');
                noise.Add(Convert.ToSingle(words[3]));

                // write text to a file
                tw3.WriteLine(text);
                //foreach (string word in words)
                //{
                //    tw.WriteLine(word);
                //}

                ...

                text = reader.ReadLine();
            }
            tw3.Close();
        }
于 2012-06-20T14:48:33.440 回答