Read the coordinate file (specify format file)

Source: Internet
Author: User

In the process of data processing, such as GPS or total station, obtained from these instruments are the specified format of the coordinate files, in the process of data processing, we often need to take these coordinates out of the processing, such as

coordinate name, 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 + + or C can be read using the scanf function, or C + + can read the getline second argument, but for C #, only the ReadLine function can read only one row at a time, so we consider splitting the read with the Split function. For the specific use of the Split function, you can take a look at http://www.cnblogs.com/yugen/archive/2010/08/18/1802781.html

   Public classPoint { Public stringname;  Publiclist<Double> list =Newlist<Double>(); }     Public classReadpoint {/// <summary>        ///represents the file absolute path/// </summary>         Public stringFilePath {Get;Set; } /// <summary>        ///Separators/// </summary>         Public stringFlag {Get;Set; }  PublicReadpoint () {//The default delimiter is ","Flag =","; }         PublicReadpoint (stringFilestringFlag =",")        {             This. FilePath =file;  This. Flag =Flag; }         PublicList<point>Read () {List<Point> list =NewList<point>(); using(StreamReader sr =NewStreamReader (FilePath)) {                //The first row is not heading, just skip overSr.                ReadLine ();  while(!Sr. Endofstream) {//read by line, then read by flag                    string[] line = Sr. ReadLine (). Split (New string[] {Flag}, stringsplitoptions.removeemptyentries); Point P=NewPoint (); //according to the format, the first one is the file nameP.name = line[0]; //may be a three-dimensional or two-dimensional, or contain a tag, elevation, and so on, so here is a list generic set//assigns the second element in the line array to the p.list until the last loop                     for(inti =1; I < line. Length; i++) {P.list.add (Double.                    Parse (Line[i])); } list.                ADD (P); }            }            returnlist; }    }

Then, in the main function, we call

        Static voidMain (string[] args) {Readpoint R=NewReadpoint (@"f:\ system files \ Desktop \1.txt"); List<Point> list=R.read ();  for(inti =0; I < list. Count; i++) {Console.Write (List[i].name+",");  for(intj =0; J < List[i].list. Count; J + +) {Console.Write (List[i].list[j]+",");            } console.readline ();        } console.readkey (); }

Console Display Results:

Read the coordinate file (specify format file)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.