比如data.txt中格式为:NCOLS 280
NROWS 332
XLLCORNER 621463.3500
YLLCORNER 3389148.9200
CELLSIZE 100.0000
NODATA_VALUE -999
24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47
24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.48 24.48 24.48 24.48
24.48 24.49 24.49 24.49 24.49 24.50 24.50 24.51 24.51 24.52 24.52
24.53 24.54 24.55 24.55 24.56 24.58 24.59 24.60 24.62 24.63 24.65
24.67 25.05 25.07 25.09 25.11 25.14 25.16 25.19 25.22 24.90 24.94
24.99 25.03 25.08 25.13 25.18 25.24 25.29 25.35 25.42 25.48 25.55
25.62 25.69 25.67 25.60 25.83 25.78 25.74 25.69 25.65 25.61 25.57
25.54 25.50 25.46 25.43 25.40 25.37 25.34 25.31 25.28 25.25 25.23
25.21 24.90 24.87 24.85 24.83 24.81 24.80 24.93 24.91 24.90 24.89
25.05 25.04 25.04 25.03 25.03 25.02 25.02 25.02 25.02 25.01 25.01
当然数据还有很多,没有贴出来
比如其中的行为332,列为280,怎么把这两个数赋给一个数组的行和列,还有要读取下面的数字放到相应的数组元素里面!

解决方案 »

  1.   

    你的文件给的不全,我按你的意思修改一下文件以方便做测试范例,文件假设为NCOLS 11
    NROWS 10
    XLLCORNER 621463.3500
    YLLCORNER 3389148.9200
    CELLSIZE 100.0000
    NODATA_VALUE -999
    24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.47 
    24.47 24.47 24.47 24.47 24.47 24.47 24.47 24.48 24.48 24.48 24.48 
    24.48 24.49 24.49 24.49 24.49 24.50 24.50 24.51 24.51 24.52 24.52 
    24.53 24.54 24.55 24.55 24.56 24.58 24.59 24.60 24.62 24.63 24.65 
    24.67 25.05 25.07 25.09 25.11 25.14 25.16 25.19 25.22 24.90 24.94 
    24.99 25.03 25.08 25.13 25.18 25.24 25.29 25.35 25.42 25.48 25.55 
    25.62 25.69 25.67 25.60 25.83 25.78 25.74 25.69 25.65 25.61 25.57 
    25.54 25.50 25.46 25.43 25.40 25.37 25.34 25.31 25.28 25.25 25.23 
    25.21 24.90 24.87 24.85 24.83 24.81 24.80 24.93 24.91 24.90 24.89 
    25.05 25.04 25.04 25.03 25.03 25.02 25.02 25.02 25.02 25.01 25.01那么对应读取方法private void TestReadFile()
    {
        using (StreamReader reader = File.OpenText("data.txt"))
        {
            string line = reader.ReadLine();//NCOLS 5
            int column_number = int.Parse(Regex.Match(line, @"(?<=NCOLS )\d+").Value);
            line = reader.ReadLine();//NROWS 6
            int row_number = int.Parse(Regex.Match(line, @"(?<=NROWS )\d+").Value);
            double[,] data = new double[row_number, column_number];
            line = reader.ReadLine();//XLLCORNER 621463.3500
            line = reader.ReadLine();//YLLCORNER 3389148.9200
            line = reader.ReadLine();//CELLSIZE 100.0000
            line = reader.ReadLine();//NODATA_VALUE -999
            int row = 0;
            line = reader.ReadLine();//第一行数据
            Regex regExp = new Regex(@"\d+(\.\d+)?", RegexOptions.Compiled);
            while (!string.IsNullOrEmpty(line))
            {
                MatchCollection mc = regExp.Matches(line);
                if (mc.Count > column_number)
                {
                    row++;
                    continue;//如果某行出错,跳过
                }
                for (int col = 0; col < mc.Count; col++)
                {
                    data[row, col] = double.Parse(mc[col].Value);
                }
                line = reader.ReadLine();//下一行数据
            }
            MessageBox.Show("done");
        }
    }方法不用任何修改,直接读你的文件,如果行列数正确,直接可以读取。
      

  2.   

    打早上起来,阅读一下。看到个bug。修改一下private void TestReadFile()
    {
        using (StreamReader reader = File.OpenText("data.txt"))
        {
            string line = reader.ReadLine();//NCOLS 5
            int column_number = int.Parse(Regex.Match(line, @"(?<=NCOLS )\d+").Value);
            line = reader.ReadLine();//NROWS 6
            int row_number = int.Parse(Regex.Match(line, @"(?<=NROWS )\d+").Value);
            double[,] data = new double[row_number, column_number];
            line = reader.ReadLine();//XLLCORNER 621463.3500
            line = reader.ReadLine();//YLLCORNER 3389148.9200
            line = reader.ReadLine();//CELLSIZE 100.0000
            line = reader.ReadLine();//NODATA_VALUE -999
            int row = 0;
            line = reader.ReadLine();//第一行数据
            Regex regExp = new Regex(@"\d+(\.\d+)?", RegexOptions.Compiled);
            while (!string.IsNullOrEmpty(line))
            {
                MatchCollection mc = regExp.Matches(line);
                if (mc.Count > column_number)
                {
                    row++;
                    continue;//如果某行出错,跳过
                }
                for (int col = 0; col < mc.Count; col++)
                {
                    data[row, col] = double.Parse(mc[col].Value);
                }
                line = reader.ReadLine();//下一行数据
                  row++;
            }
            MessageBox.Show("done");
        }
    }
      

  3.   

    出门之前,又觉得出错的话会是个bug。再次修改…
    private void TestReadFile()
    {
        using (StreamReader reader = File.OpenText("data.txt"))
        {
            string line = reader.ReadLine();//NCOLS 5
            int column_number = int.Parse(Regex.Match(line, @"(?<=NCOLS )\d+").Value);
            line = reader.ReadLine();//NROWS 6
            int row_number = int.Parse(Regex.Match(line, @"(?<=NROWS )\d+").Value);
            double[,] data = new double[row_number, column_number];
            line = reader.ReadLine();//XLLCORNER 621463.3500
            line = reader.ReadLine();//YLLCORNER 3389148.9200
            line = reader.ReadLine();//CELLSIZE 100.0000
            line = reader.ReadLine();//NODATA_VALUE -999
            int row = 0;
            line = reader.ReadLine();//第一行数据
            Regex regExp = new Regex(@"\d+(\.\d+)?", RegexOptions.Compiled);
            while (!string.IsNullOrEmpty(line))
            {
                MatchCollection mc = regExp.Matches(line);
                if (mc.Count == column_number)//数据正确才分析
                {
                    for (int col = 0; col < mc.Count; col++)
                    {
                        data[row, col] = double.Parse(mc[col].Value);
                    }
                }
                
                line = reader.ReadLine();//下一行数据
                row++;
            }
            MessageBox.Show("done");
        }
    }
      

  4.   

    dim data(100)()as string.....dim a() as string =line.split(" ")data(i+1)=a这只是一个思路,没有实现过,一是使用string.split把读取的一行按空格分解成数组,而是使用了交错数组,把分解成的数组赋值给交错数组对应的行,不知是否可行