读者你好:
关于读取记事本中的矩阵,可以参考下面的代码。
int n=0;
string[] myString = new string[4];
string str = "";
StreamReader sr = new StreamReader("test.txt");
int[,] myInt = new int[4, 3];
try
{
do
{
str = sr.ReadLine();
myString[n] = str;
n++;
} while (str != null&&n<=3);
Console.WriteLine("读出来的TXT格式");
foreach (string m in myString)
Console.WriteLine(m);
for (int i = 0; i < 4; i++)
{
string[] myNewString = myString[i].Split(' ');
for (int j = 0; j < 3; j++)
{
myInt[i, j] =int.Parse(myNewString[j]);
}
}
Console.WriteLine();
Console.WriteLine("int 数组中的数据,如下");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(myInt[i, j]+" ");
}
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
sr.Close();
}
Console.ReadLine();