2010-07-10 69 views
0

我需要一些邏輯/編程幫助,以便從文本文件中讀取多條記錄。我可以讀取線條,但是我需要在記錄完成後立即停止,將該對象推到列表中,然後繼續記錄新的記錄,直到下一個記錄出現,保存到列表中等等...從C#中的文本文件中讀取多條記錄

記錄的標題總是以G開始,作爲第一個字符。如果V(變量),d(座標),M(插入點),等等

文件內容是這樣的其餘部分:(虛擬數據)

G FEATURE01 LEVEL01 
M -10.5132 10.0000 697.5086 
V \~\@ENTITY=LINE 
V \~\@PENSTYLE=0 
V \~\@PENTHICK=1 
D -10.5089 12.0797 697.8155 
D -10.4971 13.6198 698.0429 
D -10.0399 17.3069 698.5913 
D -10.7665 11.6108 699.2279 
D -10.6769 15.9840 699.8735 
D -10.8229 13.6024 710.4438 
G FEATURE02 LEVEL02 
M -10.2681 10.0000 700.4186 
V \~\@ENTITY=LINE 
V \~\@PENSTYLE=0 
V \~\@PENTHICK=1 
D -10.2269 10.6946 700.4941 
D -10.2585 13.1788 700.7637 
D -10.2937 15.9480 701.0642 
D -10.9494 20.5230 709.1840 
D -10.9277 21.4909 709.4517 
D -10.8335 23.3862 709.9763 
G FEATURE01 LEVEL02 
M -15.4500 10.0000 700.4174 
V \~\@ENTITY=LINE 0.00 0 0.00 A A 
V \~\@PENSTYLE=0 0.00 0 0.00 A A 
V \~\@PENTHICK=1 0.00 0 0.00 A A 
D -15.5690 12.3042 700.6673 
D -15.3502 14.3130 700.8863 
D -15.1219 16.7179 701.1480 
D -15.0628 17.3409 701.2427 
D -15.5481 20.8968 709.2855 
D -15.3132 22.9163 709.8470 
D -15.1355 23.2957 709.9627 
G FEATURE03 LEVEL03 
P 0.0000 0.0000 0.0000 270.0000 90.0000 
M -12.8612 14.2951 737.6336 
V \~\@ENTITY=LINE 
V \~\@PENSTYLE=1 
V \~\@PENTHICK=1 
V @0ver1ay=KOOS 
D -13.2715 15.5321 736.5965 

所以,從上面文本文件中有4條記錄。 任何想法? 謝謝

+3

請發佈您迄今爲止編寫的代碼。人們通常不喜歡只爲你寫代碼。 – 2010-07-10 11:05:38

+0

我的代碼將佔用6頁...所以我寧願讓別人編寫邏輯。謝謝克里斯! – riaandelange 2010-07-10 16:41:26

回答

3

這裏是我試圖放在一起的一段代碼,爲了清晰而不是魯棒性等,它應該把你放在正確的軌道上。

1-我創建了一個簡單的記錄類,它將爲每條記錄(以'G'開始的行)創建,然後將所有後續行添加到此記錄,直到文件中遇到新的記錄開始。

class Record 
{ 
    public List<string> Lines { get; private set; } 
    public Record() 
    { 
    Lines = new List<string>(); 
    } 
} 

2-然後,下面的代碼將逐行處理文件,創建新記錄,並將每條記錄添加到Record集合中。

// Collection to be populated with the record data in the file 
    List<Record> records = new List<Record>(); 

    using (FileStream fs = new FileStream("datafile.dat", FileMode.Open)) 
    using (StreamReader rdr = new StreamReader(fs)) 
    { 
    string line; 

    // Read first line 
    line = rdr.ReadLine(); 
    while (line != null) 
    { 
     // Check if we have a new record 
     if (line.StartsWith("G")) 
     { 
     // We have a start of a record so create an instance of the Record class 
     Record record = new Record(); 

     // Add the first line to the record 
     record.Lines.Add(line); 

     // Read the next line 
     line = rdr.ReadLine(); 

     // While the line is not the start of a new record or end of the file, 
     // add the data to the current record instance 
     while (line != null && !line.StartsWith("G")) 
     { 
      record.Lines.Add(line); 
      line = rdr.ReadLine(); 
     } 

     // Add the record instance to the record collection 
     records.Add(record); 
     } 
     else 
     { 
     // If we get here there was something unexpected 
     // So for now just move on and read the next line 
     line = rdr.ReadLine(); 
     } 
    } 
    } 
+0

輝煌的克里斯!你是男人。謝謝... – riaandelange 2010-07-10 12:34:58