如何从txt文件中将特定数据读入数组

时间:2012-10-25 01:52:34

标签: c# regex

我正在学习C#。

现在我有一个文本文件如下(它有数千行):

    BL51 BANKED LINKER/LOCATER V6.11    10/09/2012  14:39:46  PAGE 1
    ...
    * * * * * * *   C O D E   B A N K   0   * * * * * * *
    BANK0   4000H     09A5H     UNIT         ?CO?MAIN 
    BANK0   49A5H     0289H     UNIT         ?CO?EEPROM
    BANK0   539EH     007DH     UNIT         ?PR?CSCALERPOWERSAVINGRESET?SCALER
    BANK0   541BH     00BFH     UNIT         ?PR?CSCALERRESET?SCALER
    BANK0   54DAH     06BCH     UNIT         ?PR?MODE
    ...

我想在字符串“* * * * * * * CODEBANK 0 * * * * * * *”之后读取行“BANK0”并将0x4000作为整数读入array1 [0],将0x09A5作为array2 [0 ],0x49A5进入array1 [1],0x0289进入array2 [1],依此类推。

有什么可以帮助我吗?

提前感谢您的帮助! : - )

感谢Aaron Anodide,我完成了以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Globalization;

namespace _2012_10_24_M51_file_read_into_array_test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    OpenFileDialog OpenFileDig = new OpenFileDialog();

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDig.Filter = "M51 files(*.m51)|*.m51";

        if (OpenFileDig.ShowDialog() == DialogResult.OK)
        {
            M51TextBox.Text = OpenFileDig.FileName;
        }
        else
        {
            return;
        }
    }

    // c sharp read text file into array
    /* Nomral C function */
    ArrayList M51Bank = new ArrayList();
    ArrayList M51Offset = new ArrayList();
    ArrayList M51Length = new ArrayList();

    ArrayList M51FunName = new ArrayList();
    ArrayList M51CFile = new ArrayList();

    /* C common code */
    ArrayList M51ComBank = new ArrayList();
    ArrayList M51ComOffset = new ArrayList();
    ArrayList M51ComLength = new ArrayList();

    ArrayList M51ComFunName = new ArrayList();

    static int arrayIdx1 = 0;
    static int arrayIdx2 = 0;

    private void button2_Click(object sender, EventArgs e)
    {            
        bool BankStart = false;

        string path = @"D:\xxxx.m51";
        string[] ReadText = File.ReadAllLines(path);            

        for (int idx = 0; idx < ReadText.Length; idx++)
        {
            if (ReadText[idx].Contains("*   C O D E   B A N K   0   *"))
            { 
                // found it do 
                BankStart = true;
            }

            if (ReadText[idx].Contains("OVERLAY MAP OF MODULE:"))
            {
                // found it do 
                BankStart = false;
            }

            if (BankStart == true)
            {
                if ((ReadText[idx].Contains("BANK0")
                        || ReadText[idx].Contains("BANK1")
                        || ReadText[idx].Contains("BANK2")
                        || ReadText[idx].Contains("BANK3")
                        || ReadText[idx].Contains("BANK4")
                        || ReadText[idx].Contains("BANK5")
                        || ReadText[idx].Contains("BANK6")
                        || ReadText[idx].Contains("BANK7")
                        || ReadText[idx].Contains("BANK8")
                        || ReadText[idx].Contains("BANK9")
                        || ReadText[idx].Contains("BANK10")
                        || ReadText[idx].Contains("BANK11")
                        || ReadText[idx].Contains("BANK12")
                        || ReadText[idx].Contains("BANK13")
                        || ReadText[idx].Contains("BANK14")
                        || ReadText[idx].Contains("BANK15"))
                    && (ReadText[idx].Contains("?CO?") != true))
                {
                    BANKProc(ReadText[idx]);
                }                        
            }               
        }

        /* */
        int[] m51bank = (int[])M51Bank.ToArray(typeof(int));
        int[] m51Offset = (int[])M51Offset.ToArray(typeof(int));
        int[] m51Length = (int[])M51Length.ToArray(typeof(int));
        string[] m51FunName = (string[])M51FunName.ToArray(typeof(string));
        string[] m51CFile = (string[])M51CFile.ToArray(typeof(string));

        for (int i = 0; i < M51Bank.Count; i++)
        {
            textBox1.Text += (int)m51bank[i] + " ";

            richTextBox1.Text += m51bank[i].ToString("X2");
            richTextBox1.Text += m51Offset[i].ToString("X2") + "\t";
            richTextBox1.Text += m51Length[i].ToString("X2") + "\t";
            richTextBox1.Text += m51FunName[i] + "\t";
            richTextBox1.Text += m51CFile[i] + "\r\n";
        }                                       
    }

    private void BANKProc(string p)
    {            
        string[] NumberSplit = p.Split(new char[]{' ', '\t', 'H','K'},StringSplitOptions.RemoveEmptyEntries);

        string[] NameSplit = p.Split(new char[]{'?'},StringSplitOptions.RemoveEmptyEntries);

        if (NameSplit.Length == 4)
        {
            M51Bank.Add(int.Parse(NumberSplit[1], NumberStyles.HexNumber));
            M51Offset.Add(int.Parse(NumberSplit[2], NumberStyles.HexNumber));
            M51Length.Add(int.Parse(NumberSplit[3], NumberStyles.HexNumber));

            M51FunName.Add(NameSplit[2]);
            M51CFile.Add(NameSplit[3]);

            arrayIdx1++;
        }
        else if (NameSplit.Length == 3)
        {
            M51ComBank.Add(int.Parse(NumberSplit[1], NumberStyles.HexNumber));
            M51ComOffset.Add(int.Parse(NumberSplit[2], NumberStyles.HexNumber));
            M51ComLength.Add(int.Parse(NumberSplit[3], NumberStyles.HexNumber));

            M51ComFunName.Add(NameSplit[2]);

            arrayIdx2++;
        }
    }
}

}

3 个答案:

答案 0 :(得分:1)

使用File.ReadAllLines&amp; Split

string path = @"c:\temp\MyTest.txt";
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
    string [] split = s.Split(new Char [] {'\t' });
    Console.WriteLine(split[0]);
}

答案 1 :(得分:1)

我为你做了一些提示,假设你不想让别人为你做这项工作。如果您认为这对您有帮助,请随时提出相关问题。

  

我想用“BANK0”

读取行开头
  • “该行”是一个提示,您希望一次处理一行文本
    • 查看File.ReadAllLines()
  • “以BANK0开头”暗示您要使用条件逻辑和字符串比较
  字符串后面的

“* * * * * * * C O D E B A N K 0 * * * * * * *”

  • 这是一个提示,你的程序将有状态
    • state1:​​尚未遇到CODEBANK行
    • state2:遇到过CODEBANK行
  

将0x4000作为整数导入array1 [0],将0x09A5作为array2 [0],将0x49A5作为array1 [1],将0x0289作为array2 [1],依此类推。

  • 您需要按顺序对每行文本进行进一步处理 从中提取单个字段值
    • 查看String.Split()
  • 您需要转换十六进制数字的字符串表示形式 到实际值(值的类型将由array1和array2的类型给出)
    • 看看int.Parse()

答案 2 :(得分:0)

使用File.ReadAllLines()和String.StartsWith()和String.Split()方法:

foreach(string line in File.ReadAllLines())
{
   if(line.StartsWith("BANK0")
   {
       string[] words = line.Split("   "); // now these words can be put into their respective arrays
   }
}