zoukankan      html  css  js  c++  java
  • 读取坐标文件(指定格式文件)

        在数据处理过程,比如GPS或者全站仪,从这些仪器中得到都是规定格式的坐标文件,在进行数据处理过程中,我们经常需要把这些坐标取出来进行处理,比如

    坐标名,x,y
    01,23.4,33.2
    02,23.4,33.2
    03,23.4,33.2
    04,23.4,33.2
    05,23.4,33.2
    06,23.4,33.2
    07,23.4,33.2

    C++或者C都可以使用scanf函数来读取,或者C++可以对getline第二个参数传值读取,但是对于C#,只有readline函数,每次只能读取一行,所以我们考虑使用split函数来分割读取。关于split函数具体用法,大家可以看一下http://www.cnblogs.com/yugen/archive/2010/08/18/1802781.html

      public class Point
        {
            public string name;
            public List<double> list = new List<double>();
        }
        public class ReadPoint
        {
            /// <summary>
            /// 表示文件绝对路径
            /// </summary>
            public string FilePath { get; set; }
            /// <summary>
            /// 分隔符
            /// </summary>
            public string Flag { get; set; }
            public ReadPoint()
            {
                //默认分隔符为“,”
                Flag = ",";
            }
            public ReadPoint(string file, string flag = ",")
            {
                this.FilePath = file;
                this.Flag = flag;
            }
    
            public List<Point> Read()
            {
                List<Point> list = new List<Point>();
                using (StreamReader sr = new StreamReader(FilePath))
                {
                    //第一行未标题,直接跳过去
                    sr.ReadLine();
                    while (!sr.EndOfStream)
                    {
                        //按行读取,然后按照Flag读取
                        string[] line = sr.ReadLine().Split(new string[] { Flag }, StringSplitOptions.RemoveEmptyEntries);
                        Point p = new Point();
                        //根据格式可知,第一个为文件名
                        p.name = line[0];
                        //可能为三维坐标,也可能为二维,或者含有标记码,高程之类,所以这里采用一个list泛型集合
                        //将line数组中的第二个元素一直到最后循环赋给p.list
                        for (int i = 1; i < line.Length; i++)
                        {
                            p.list.Add(double.Parse(line[i]));
                        }
                        list.Add(p);
                    }
                }
                return list;
            }
        }

        然后,在main函数中我们调用一下

            static void Main(string[] args)
            {
                ReadPoint r = new ReadPoint(@"F:系统文件桌面1.txt");
                List<Point> list=r.Read();
                for (int i = 0; i < list.Count; i++)
                {
                    Console.Write(list[i].name+",");
                    for (int j = 0; j < list[i].list.Count; j++)
                    {
                        Console.Write(list[i].list[j] + ",");
                    }
                    Console.ReadLine();
                }
                Console.ReadKey();
            }

    控制台显示结果:

  • 相关阅读:
    B
    I
    C
    判断线段之间的关系(D
    C
    求矩形的周长(线段树+扫描线) Picture POJ
    面积并+扫描线 覆盖的面积 HDU
    线段树->面积并 Atlantis HDU
    E1. Array and Segments (Easy version)(暴力) && E2. Array and Segments (Hard version)(线段树维护)
    Python File writelines() 方法
  • 原文地址:https://www.cnblogs.com/wzxwhd/p/5861054.html
Copyright © 2011-2022 走看看