我有一个文本文件,其中有一些要点:
20,30
5,40
67,34
2,0
98,34
如何将这个点添加到列表中?这是我编写的代码:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text document (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
_mn.nazwa_pliku.Text = filename;
int num = 0; //get a number of points/line in file
for (int i = 0; i < num; i++) //for everyone line/point
{
int x = 0; //get X-value from file
int y = 0; //get Y-value from file
Klaster klaster = new Klaster();
klaster.Punkty.Add(new Point(x, y));
Klastry.Add(klaster);
}
}
最佳答案
void LoadCordinates()
{
StreamReader sr = new StreamReader("PATH OF FILE");
Klaster klaster = new Klaster();
while(sr.EndOfStream == false)
{
string temp = sr.ReadLine();
if(temp.Contains(',') && temp.Split(',').Length == 2)
{
klaster.Punkty.Add(new Point(int.Parse(temp.Split(',')[0].Trim()), int.Parse(temp.Split(',')[0].Trim())));
Klastry.Add(klaster);
}
}
}
关于c# - 如何从文本文件中获取 2 个数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017094/