我需要更改代码以了解数组在txt文件中的维数,我的代码中只能得到2d数组的维数的问题,例如2 * 2,3 * 3 ...但我需要例如3 * 1在txt文件中读取未知数组维度的双数
static double[,] ParseNumberFile(string filename)
{
// read data from txt file
string fileContent = File.ReadAllText(filename);
// first put all lines into an string array
string[] allLines = fileContent.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
// calculate 2D array's dimension lengths, and initialize the 2Darray
int rowCount = allLines.Length;
int columnCount = allLines.Length;
int i = 0, j = 0;
double[,] result = new double[rowCount, columnCount];
foreach (var row in fileContent.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
j = 0;
foreach (var col in row.Trim().Split(' '))
{
result[i, j] = double.Parse(col.Trim(), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo);
j++;
}
i++;
}
return result;
}
http://msdn.microsoft.com/en-us/library/2s05feca.aspx – Leri 2013-04-30 11:47:13
注意:有[File.ReadAllLines](http://msdn.microsoft.com /en-us/library/s2tte0y1.aspx) – 2013-04-30 11:53:07
你能否提供一些来自你txt的示例文本? – WiiMaxx 2013-04-30 12:12:08