在C#中将文本文件读入数组

时间:2011-02-27 11:43:11

标签: c# programming-languages

这是需要处理的文本文件的摘录。需要做的是该计划 必须阅读此文本文件并将其格式化为specfication。问题是我没有太多的经验工作 与文本文件。这是示例输入文件。

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,名称和活动配置文件。 如果在文件中找到每次操作员ID,则需要将结果打印到新行。如果用户超过1 活动配置文件,操作员ID和名称和配置文件必须输出到新行。如果用户具有禁用的配置文件 必须忽略数据。从示例中可以看出,第一个单元被忽略,因为它们被禁用。该 具有启用的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 个答案:

答案 0 :(得分:0)

使用文本阅读器,您可以通过调用TextReader的ReadLine方法读取每一行。

您暂时执行此操作(!textreader.EndOfFile)

对于每一行,您可以搜索特定字符 - >搜索=并对其后面的文字做一些事情。

您还可以检查该行是否以操作员ID

开头

答案 1 :(得分:-1)

您可以使用正则表达式找到您要找的东西..

首先将整个文件加载到流中。 然后使用这样的正则表达式:

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

将找到所有帖子,然后你需要对数组进行另一个正则表达式检查以提取activeProfiles(并查看是否有超过1个)

类似的东西:

@"Active profile = (.+?) "

disclamer ..您可能需要修改正则表达式以适应流读取器实际放入流中的内容。

Here是检查您的需求

的好工具

编辑:以下是该计划的样子:

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();


        }
    }