2011-02-27 78 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