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