2011-02-27 84 views
1

這是需要處理的文本文件的摘錄。需要完成的是程序 必須在這個文本文件中讀取並將其格式化爲一個規範。問題是我沒有太多的工作 與文本文件的體驗。這是示例輸入文件。在C#中將文本文件讀入數組中

BSA  Security Definition - Operator Report Type 28 


NBC 3RD QUARTER PROFILE REVIEW 2010 
________________________________________________________________________________   


Operator:     ABAZ095  
Number of entries:  149 
User selection:   Selected Items   

________________________________________________________________________________   


Search Criteria :- 

    Operator Name = BT% 
    Approval Status = Any 
    Enable Status = Any 
    One-time Pwd = Any 
    Profiles  = 
    Units   = 
    Printers  = 
    Terminals  = 

________________________________________________________________________________   

Operator ID   = BTA020   

Name     = ASIA CHAMBEGA 
Active profile  = User_Disabled 
Enable status   = Disabled   
Re-enable date  = 31/12/36 00:00:00   
Approval status  = Approved   
Last changed   = 21/07/10 07:34:30  
Last sign-on   = 13/06/08 14:09:37   
Calculated pwd  = BD    
One-time password  = No    
Assigned unit   = None    



Operator ID   = BTAC002   

Name     = A KALATA (NBC) 
Active profile  = User_Disabled 
Enable status   = Disabled   
Re-enable date  = 31/12/36 00:00:00   
Approval status  = Approved   
Last changed   = 31/05/10 14:04:41  
Last sign-on   = n/a 
Calculated pwd  = B9    
One-time password  = No    
Assigned unit   = None    



Operator ID   = BTAK000   

Name     = AISHA KEJO  
Active profile  = NLCB_R6.0_ACCESSCTRL  
Active profile  = NLCB_R6.0_VERAUT_MBE  
Enable status   = Enabled   
Re-enable date  = n/a 
Approval status  = Approved   
Last changed   = 12/07/08 08:10:47  
Last sign-on   = 19/07/08 08:08:58   
Calculated pwd  = 8A    
One-time password  = No    
Assigned unit   = NLCB    



Operator ID   = BTAL001   

Name     = AMANDUS LIPILI 
Active profile  = User_Disabled 
Enable status   = Disabled   
Re-enable date  = 31/12/36 00:00:00   
Approval status  = Approved   
Last changed   = 01/07/10 08:39:03  
Last sign-on   = 11/11/09 08:25:07   
Calculated pwd  = 4B    
One-time password  = No    
Assigned unit   = None    

當處理的輸出文件應如下所示:

BTAK000, AISHA KEJO, NLCB_R6.0_ACCESSCTRL 
BTAK000, AISHA KEJO, NLCB_R6.0_VERAUT_MBE 

正如你所看到的,所需要的一切數據,但只有運營商ID,姓名和活動的配置文件需要輸出被拉。 如果在文件中找到每個操作員標識,則結果需要打印到新行。如果用戶有超過1 活動配置文件,則必須將操作員標識和名稱和配置文件輸出到新行。如果用戶具有禁用的配置文件 ,則必須忽略數據。正如您從示例中所看到的,第一個單元被忽略,因爲它們被禁用。具有啓用satatus的 用戶就是例子。正如你可以看到的輸出示例。

我的想法是將數據拉到數組中,但只輸出操作員ID,名稱和配置文件。我該怎麼做呢?

這是我到目前爲止有:

Console.WriteLine("Enter Input File Location: " + "\n"); 

      //Reads path specifed by the user for input. 
      string t = File.ReadAllText(Console.ReadLine()); 

      //Splits file where there is an equals sign. 
      t = t.Replace("=", ""); 
      //Removes all tabbed spaces. 
      t = t.Replace("\t", ""); 
      //Removes any new lines. 
      t = t.Replace("\n", ","); 
      //Removes blank spaces. 
      t = t.Replace(" ", ""); 
      //Removes the Underscore. 
      t = t.Replace("_", ""); 

      //Removes any leading or trailing whitespaces. 
      t = t.Trim(','); 

      //Writes formatted file to a pre defined ouput file's location. 
      File.WriteAllText(@"C:/3rd Quarter1.txt", t); 
+2

請張貼鱈魚e迄今爲止你寫的。人們通常不喜歡只爲你寫代碼。事實上,這是一個工作描述,而不是一個問題。 – 2011-02-27 11:45:01

+0

沒有人會爲你做你的工作。試一試,回來**具體的**編程問題。例如:「我如何從文本文件中讀取信息」或「如何將此文本行分隔爲多個部分,使用冒號作爲分隔符」 – jgauffin 2011-02-27 11:48:25

+0

好的,我在這裏有代碼。它所做的基本上是拆分,刪除等號並對文本進行格式化,以便它從新行開始,即時遇到問題的是我不知道如何有選擇地選擇要輸出的信息。繼承人迄今爲止:見編輯quaeston – Paveetren 2011-02-27 12:08:24

回答

0

隨着您可以通過調用的TextReader的ReadLine方法讀取每一行一個TextReader的。

你在一段時間(!textreader.EndOfFile)

對於每一行,您可以搜索特定的字符做到這一點 - >搜索=和用它做後面的文本的東西。

您還可以檢查是否符合運營商ID

-1

開始,你可以使用正則表達式來查找你的整個文件加載到流尋找..

開始的東西。 再使用這樣的正則表達式:

@"Operator ID = (.+?) Name = (.+?) Active profile = (.+?) Enable status " 

會發現所有的職位,那麼你OFC需要做陣列另一個正則表達式檢查,以提取activeProfiles(並查看是否有多於1)

是這樣的:

@"Active profile = (.+?) " 

disclamer ..你可能需要修改的正則表達式來適應一下StreamReader的其實是把到流。

Here是一個很好的工具來檢查你的reqex的

編輯:這是方案如何能看起來像一個示例:

static void Main(string[] args) 
    { 
     string path = @"c:\test.txt"; 
     string t = File.ReadAllText(path); 
     string pattern1 = @"OperatorID=(.+?),,Name=(.+?),(.+?),Enablestatus"; 
     Regex rgx = new Regex(pattern1); 

     //Removes all tabbed spaces. 
     t = t.Replace("\t", ""); 
     //Removes any new lines. 
     t = t.Replace("\n", ","); 
     //Removes blank spaces. 
     t = t.Replace(" ", ""); 
     //Removes the Underscore. 
     t = t.Replace("_", ""); 
     t = t.Replace("\r", ""); 

     MatchCollection matches = rgx.Matches(t); 
     List<string[]> test = new List<string[]>(); 
     foreach (var match in matches) 
     { 
      string[] newString = match.ToString().Split(new string[] { @"OperatorID=", @",,Name=", @",Activeprofile=", @",Enablestatus", }, StringSplitOptions.RemoveEmptyEntries); 

      for (int i = 3 ; i <= newString.Length; i++) 
      { 
       test.Add(new string[] { newString[0], newString[1], newString[i - 1] }); 
      } 

     } 

     foreach (var line in test) 
     { 
      Console.WriteLine("ID: {0}\nName: {1}\nActive Profile: {2}\n", line[0], line[1], line[2]); 
     } 

     Console.ReadKey(); 

沒有給予很多的關注細節,這壽:)

編輯:要將結果添加到文件:

 using (StreamWriter myWriter = new StreamWriter(@"c:\testOutput.txt")) 
    { 

     foreach (var line in test) 
     { 
      myWriter.WriteLine("ID: {0}", line[0]); 
      myWriter.WriteLine("Name: {0}", line[1]); 
      myWriter.WriteLine("Active Profile: {0}", line[2]); 
      myWriter.WriteLine(); 


     } 
    } 
+0

好的,這種解決方案出色.....我還有一個問題。我想要將寫入控制檯的內容寫入新的文本文件。我使用了下面的代碼,但它只將ID寫入文件:File.WriteAllLines(@「C:/ 3rd Quarter1.txt」,test [0]);我究竟做錯了什麼?我嘗試了WriteAllText方法,但是會引發buid錯誤。 – Paveetren 2011-02-27 17:20:28

+0

爲什麼有人會在沒有給出理由的情況下降低答案..我的答案意味着我如何做到這一點的最佳想法..這種社交是關於一起學習,並幫助體力勞動學會..所以,如果你降級我,添加一個理由讓我可以從我自己的錯誤中學習 – Tirdyr 2011-02-27 19:26:30