Reputation: 75
My program is not looping through the files. The program correctly writes all the names of the files into 'list of files'. I then expect it to output the mean, min and max values of the list of data in each file. However, whenever it runs, it outputs the mean, min and max values for only one file. I think that the looping is not working very well.
I tried creating additional loop between textwriter tw3 and its closing bracket but this did not work. I also tried the same with tw4 but again it did not work. I am not sure whether the problem is due to the looping OR whether I am not using the right syntax to call each of the files in the folder. The whole code is below.
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)
{
}
}
}
I will be very grateful for anybody help!!
Upvotes: 3
Views: 135
Reputation: 10044
As Reniuz pointed out, when you are having this kind of problem, you should step through your code. In Visual studio, it is as difficult as setting a breakpoint (F9) where you want to start stepping through, and pressing F11 repeatedly once the debugger reaches the point.
In your case, however, the two errors are from your while loop and foreach loop. The correct version of the loops as well as the full code is shown below:
While loop should be:
// 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();
}
Second loop should be:
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));
}
And here goes the full code
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();
}
Upvotes: 1
Reputation: 4888
Like you said, your loops don't look right to me. Why don't you try out something like this ("..." represent sections of your code that I didn't copy for the sake of brevity)? Note that there is a single while (text != null) with a text = reader.ReadLine(); at the end in stead of the multiple whiles and ifs in your example.
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();
}
Upvotes: 1
Reputation: 8511
You initialize your numbers (mean,mode,sum,count etc.) inside the for loop that iterates over your files, so they are reset for each file.
Initialize them outside the loop and it should work. (As others have pointed out there are other issues with the code as far as not looking very clean, but that is the underlying problem.)
Upvotes: 1