내 프로그램이 파일을 루핑하지 않습니다. 프로그램은 파일의 모든 이름을 '파일 목록'에 올바르게 기록합니다. 그런 다음 각 파일의 데이터 목록의 평균, 최소 및 최대 값을 출력 할 것으로 예상합니다. 그러나 실행될 때마다 하나의 파일에 대한 평균, 최소 및 최대 값을 출력합니다. 루핑이 잘 작동하지 않는다고 생각합니다.여러 파일에 액세스 중
텍스트 작성기 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)
{
}
}
}
나는 누군가의 도움을 매우 감사하게 될 것입니다!
정리 꽤 사용할 수있는이 코드 ... 모든 응시자 간다? :) –
코드를 통해 뭘했는지 보셨습니까? – BugFinder
[FileInfo 클래스] (http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx)를 사용해야하며, 더 쉽게 사용할 수있는 많은 메서드에 액세스 할 수 있습니다 . [기타 참고 사항] (http : // www.dotnetperls.com/fileinfo). 루프를 사용하면 훨씬 쉽게 읽을 수 있고 쉽게 따라 할 수 있습니다. – Amicable