【发布时间】:2015-11-15 02:13:06
【问题描述】:
我正在尝试读取文本文件并将其数据存储到数组列表中。它的工作没有任何错误。我的文本文件里面是这样的。
277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23
但在控制台输出中我可以看到这么多数据。
277.18
311.13
349.23
349.23
**329.63
329.63
293.66
293.66
261.63
293.66
329.63
349.23
392**
277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23
我的文本文件中也没有粗体数字。 这是我的代码。如何解决这个问题?谁能帮帮我..拜托...
OpenFileDialog txtopen = new OpenFileDialog();
if (txtopen.ShowDialog() == DialogResult.OK)
{
string FileName = txtopen.FileName;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString());
while ((line = file.ReadLine()) != null)
{
list.Add(double.Parse(line));
}
//To print the arraylist
foreach (double s in list)
{
Console.WriteLine(s);
}
}
【问题讨论】:
-
您是否使用调试器完成了循环?
-
你的变量 FileName 已经是一个字符串,所以你不需要调用 ToString()。局部变量也应该以小写字母开头。
-
@shona92 您的代码是正确的,但您的文本文件格式值得怀疑。您需要按照输入中显示的方式设置每一行。
-
旁注:将
System.IO.StreamReader放入使用,即using(System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString())) {...} -
@X-TECH 文本文件格式为 .txt,每个数字都从新行开始。
标签: c# text arraylist streamreader