C# 从txt文件中读取数据,放在数组中

maomao131418 2014-04-03 09:41:13
如题。
txt中的数据是通过以下代码存进去的。

StreamWriter sw = new StreamWriter(filePath1, true);
for (int m = 0; m < hang; m++)
{
for (int n = 0; n < lie; n++)
{ sw.WriteLine(averwave[m, n]);

}

}

averwave[m, n]是float型的二维数组。
写在txt中的是如下格式:

1532.379
1537.846
1542.652
1546.845
1551.982
1557.646
1563.63
1568.33
1532.253
1536.746
1541.891
1546.446
1552.784
1558.362
1562.293
1567.901
1533.664
1537.608
1543.62
1547.411
1550.802
1556.53
1562.183
1568.468
1531.963
1537.224
1541.882
1545.743
1552.373
1557.944
1562.089
1566.887
1532.543
1536.154
1543.346
1547.646
1553.595
1558.095
1562.02
1566.951
1532.274
1536.522
现在要怎么读出来放在一维数组中 呢。想用streamreader来做的,可是好像不对啊
还希望有大神指点下哈。
...全文
1665 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
maomao131418 2014-04-22
  • 打赏
  • 举报
回复
谢谢各位! 结贴
wind_cloud2011 2014-04-03
  • 打赏
  • 举报
回复


            string path=@"d:\1.txt";
            string[] a = File.ReadAllLines(path);
            float[] b=new float[a.Length];
            for(int i=0;i<a.Length;i++)
            {
               b[i]=float.Parse(a[i]);
            }

threenewbee 2014-04-03
  • 打赏
  • 举报
回复
float[] data = File.ReadAllLines("c:\\1.txt").Select(x => float.Parse(x)).ToArray();
宝_爸 2014-04-03
  • 打赏
  • 举报
回复
引用 4 楼 maomao131418 的回复:
[quote=引用 3 楼 findcaiyzh 的回复:] 这个简单: File.ReadAllLines Opens a text file, reads all lines of the file, and then closes the file. public static string[] ReadAllLines( string path ) 参考: http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
可以直接读取float型的嘛?因为是以float型的直接写入,如果这样读取就变成了string型的。这样还要再进行数据转换[/quote] 这个应该不行,你得自己转。 Txt文件,哪有类型一说。 你可以用Serializer,序列化和反序列化,这样应该有类型信息。
maomao131418 2014-04-03
  • 打赏
  • 举报
回复
引用 3 楼 findcaiyzh 的回复:
这个简单: File.ReadAllLines Opens a text file, reads all lines of the file, and then closes the file. public static string[] ReadAllLines( string path ) 参考: http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
可以直接读取float型的嘛?因为是以float型的直接写入,如果这样读取就变成了string型的。这样还要再进行数据转换
宝_爸 2014-04-03
  • 打赏
  • 举报
回复
这个简单: File.ReadAllLines Opens a text file, reads all lines of the file, and then closes the file. public static string[] ReadAllLines( string path ) 参考: http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
sj490790083 2014-04-03
  • 打赏
  • 举报
回复
File.ReadAllLines(path),然后对返回的string[]遍历每一个元素,element.split('.')拆分,再添加至一维数组
宝_爸 2014-04-03
  • 打赏
  • 举报
回复
一行一行读。或者一次读完,在split.

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧