首页 > Windows开发 > 详细

C#读取文件中的浮点数据

时间:2020-06-30 21:52:35      阅读:105      评论:0      收藏:0      [点我收藏+]
string path = "test.txt";
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);

char[] separator = new char[] { ‘ ‘, ‘,‘, ‘\t‘, ‘\r‘, ‘\n‘ };
string txt = sr.ReadToEnd().Trim().Replace("\r\n\r\n", "\r\n");
string[] lines = txt.Split(separator);

List<float> result = new List<float>(1024);
string[] nums;
float tmp;

for (int i = 0; i<lines.Length; i++)
{
    nums = lines[i].Trim().Split(separator);
    foreach(var num in nums)
    {
        if (num == "")
            continue;
        if(float.TryParse(num, out tmp))
        {
            result.Add(tmp);
        }
        else
        {
            Debug.Log("Format error at line " + (i + 1)+ ": " + num);
        }
    }
}

sr.Close();
fs.Close();

  

C#读取文件中的浮点数据

原文:https://www.cnblogs.com/yl-xy/p/13215542.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!