111,120
社区成员
发帖
与我相关
我的任务
分享private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("test.txt", FileMode.Open);
int index = 7;//取第几行数据,从0开始,此处可由用户输入
const int length = 32;//每行的长度,15个字符+15个空格+回车换行2=32
byte[] buff = new byte[length];
long line = index * length;//计算第index行前面占用的字节数
fs.Seek(line, SeekOrigin.Begin);//移动文件流指针位置
fs.Read(buff, 0, length);//从指定位置读取指定长度字节赋值给buff
fs.Close();
for (int i = 0; i < buff.Length; i+=2)//i++保留空格,i+=2去掉空格
{
Console.Write((char)buff[i]);//输出结果符合要求,buff就是要的结果
}
}
string _Text = @"0 0 0 0 0 0 0 6 0 0 0 0 0 0 0
0 0 0 0 0 0 6 0 0 0 0 4 0 0 0
0 0 0 5 0 0 0 0 0 0 0 4 0 0 0
0 0 0 1 6 1 1 1 6 1 1 1 1 0 0
0 0 0 1 0 0 0 0 2 2 2 2 1 0 0
4 4 4 1 0 4 0 3 2 2 2 2 1 0 7
0 0 0 3 0 3 0 5 4 2 2 2 1 0 0
0 0 0 3 0 0 0 0 0 0 0 0 1 0 0
0 0 0 1 0 0 6 0 0 0 0 4 1 0 0
0 0 0 3 1 1 0 1 1 3 1 1 0 0 3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 3
0 0 4 0 0 0 0 0 7 4 0 0 0 0 0
0 0 0 0 4 0 0 0 0 3 0 0 7 0 0
0 4 0 3 0 0 0 0 0 0 0 0 7 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##############################
0 0 0 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 7 0 0 0 0 0 0 0 7 0 0
0 0 0 0 0 0 0 7 0 0 0 0 0 0 0
0 0 7 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 7
0 0 0 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 7 0 0 7 0 0 0 0 0 7
0 0 7 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 0 0
0 0 7 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 7
##############################";
string[] _List = _Text.Split(new char[] { '\r', '\n', '#' }, StringSplitOptions.RemoveEmptyEntries);
IList<byte[,]> _BytesList = new List<byte[,]>();
byte[,] _Bytes = new byte[15, 15];
int _ReadIndex = 0;
for (int i = 0; i != _List.Length; i++)
{
if (_List[i].Trim().Length == 0) continue;
string[] _BytesText = _List[i].Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (_BytesText.Length != 15) continue;
for (int z = 0; z != 15; z++)
{
_Bytes[_ReadIndex, z] = Convert.ToByte(_BytesText[z]);
}
_ReadIndex++;
if (_ReadIndex == 15)
{
_BytesList.Add(_Bytes);
_ReadIndex = 0;
}
}