0

我正在学习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++;
        }
    }
}

}

4

3 回答 3

1

使用File.ReadAllLines & 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]);
}
于 2012-10-25T01:57:11.940 回答
1

我为你做了一些提示,假设你不希望有人为你做这项工作。如果您认为这可能对您有所帮助,请随时提出有关此问题的问题。

我想阅读以“BANK0”开头的行

  • "the line" 提示您要一次处理一行文本
    • 看看 File.ReadAllLines()
  • "start with BANK0" 提示您要使用条件逻辑和字符串比较

在字符串“* * * * * * * CODEBANK 0 * * * * * * *”之后

  • 这是您的程序将具有状态的提示
    • state1:​​尚未遇到CODEBANK行
    • state2:遇到CODEBANK线

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

  • 您需要对每一行文本进行进一步处理,以便从中提取单个字段值
    • 看看 String.Split()
  • 您需要将十六进制数字的字符串表示形式转换为实际值(值的类型将由array1和array2的类型给出)
    • 看看 int.Parse()
于 2012-10-25T02:33:28.993 回答
0

使用 File.ReadAllLines() 和 String.StartWith() 和 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
   }
}
于 2012-10-25T02:11:38.230 回答