a.txt中的内容格式为:0,0,0.1//表示第0行第0列是0.1
0,1,0.2
1,0,0.3
1,1,0.4怎样从txt中读取内容并存入数组int[2,2] A 中?
最好有整个代码,谢了~~小女感激不尽

解决方案 »

  1.   

    readline方法读出一行,然后自己处理
      

  2.   

    StreamReader 一行一行的读split 到 数组中
    然后分析 填充到 int[2,2] A 中
      

  3.   

      using (StreamReader sr2 = new StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312"), false))
        {
            while (sr2.ReadLine()!= null)
                {
    //放入数组
                       }
    }
      

  4.   

    0,0,1
    0,1,2
    1,0,3
    1,1,4
    存放在G盘的ayyar.txt中    class Program
        {
            static void Main(string[] args)
            {
                GetNumber(@"G:\array.txt");
                Console.Read();
            }        private static void GetNumber(string filePath)
            {
                int row = 2;
                int column = 2;
                int[,] intArray = new int[row, column];
                try
                {
                    using (StreamReader sr = new StreamReader(filePath))
                    {
                        while (!sr.EndOfStream)
                        {
                            string str = sr.ReadLine();
                            string[] strArray = str.Split(',');
                            intArray[int.Parse(strArray[0]), int.Parse(strArray[1])] = int.Parse(strArray[2]);
                        }
                    }
                }
                catch(Exception e)
                {
                    string msg = e.Message;
                }
                for (int i = 0; i < column; i++)
                {
                    for (int j = 0; j < row; j++)
                    {
                        Console.Write(intArray[i,j].ToString() + "  ");
                    }
                    Console.Write("\n");
                }
            }
        }
      

  5.   

    private static void GetNumber(string filePath)
            {
                int row = 2;
                int column = 2;
                int[,] intArray = new int[row, column];
                try
                {
                    using (StreamReader sr = new StreamReader(filePath))
                    {
                        while (!sr.EndOfStream)
                        {
                            string str = sr.ReadLine();
                            string[] strArray = str.Split(',');
                            intArray[int.Parse(strArray[0]), int.Parse(strArray[1])] = int.Parse(strArray[2]);
                        }
                    }
                }
                catch(Exception e)
                {
                    string msg = e.Message;
                }
                for (int i = 0; i < column; i++)
                {
                    for (int j = 0; j < row; j++)
                    {
                        Console.Write(intArray[i,j].ToString() + "  ");
                    }
                    Console.Write("\n");
                }
            }