zoukankan      html  css  js  c++  java
  • C#读取文件中的浮点数据

    string path = "test.txt";
    FileStream fs = new FileStream(path, FileMode.Open);
    StreamReader sr = new StreamReader(fs);
    
    char[] separator = new char[] { ' ', ',', '	', '
    ', '
    ' };
    string txt = sr.ReadToEnd().Trim().Replace("
    
    ", "
    ");
    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();
    

      

  • 相关阅读:
    网络
    DB
    DevOps
    Linux 进程管理:Supervisor
    Tomcat相关知识
    Tomcat配置和数据源配置
    Eclipse智能提示及部分快捷键
    Servlet工作原理
    蜗牛
    Servlet技术
  • 原文地址:https://www.cnblogs.com/yl-xy/p/13215542.html
Copyright © 2011-2022 走看看