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)