日期:2014-05-17  浏览次数:20803 次

从text读内容存到二维数组使用的问题
点 纬度 经度 海拔
0, 40.123, -1.231, 10.59
1, 40.230, -1.242, 10.51
2, 40.235, -1.2425, 0

text文件中存放这样一组数据。运行时输入两个点(例如0,1),计算这两个点的距离。
怎么从text读内容存到二维数组

暂时只写成这样。。
C# code

 static void Main(string[] args)
        {
            string[][] Coor;    //存放坐标的二维数组
            Coor = new string[3][];
            TextReader tr = new StreamReader(@"E:\nottingham\study\PRG\Projects\distance_P2P\distance_P2P\Coordinate.txt");
            
            string rd="";
            while ((rd = tr.ReadLine()) != null)
            {
                string[] row = rd.Split(',');
                
            }
            
        }



------解决方案--------------------
File.ReadAllLines("")
再split分割
------解决方案--------------------
foreach(string s in File.ReadAllLines(""))
{
 string[] arr=s.Split(',');

}
------解决方案--------------------
C# code
        string[] rows = File.ReadAllLines(@"E:\nottingham\study\PRG\Projects\distance_P2P\distance_P2P\Coordinate.txt");
        string[][] values = new string[rows.Length][];
        for (int i = 0; i < rows.Length; i++)
            values[i] = rows[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //以下是输出各元素的值
        for (int i = 0; i < values.Length; i++)
        {
            for (int j = 0; j < values[i].Length; j++)
                Console.Write(values[i][j] + "    ");
            Console.WriteLine();
        }

------解决方案--------------------
string[] temp = System.IO.File.ReadAllLines("E:\\aaa.txt", System.Text.Encoding.GetEncoding("gb2312"));
Dictionary<int, product> dic = new Dictionary<int, product>();
foreach (string s in temp)
{
string[] pInfo = s.Split(',');
int id = int.Parse(pInfo[1]);
if (!dic.ContainsKey(id))
{
product p = new product();
p.id = id;
p.name = pInfo[2];
p.count = int.Parse(pInfo[3]);
p.remarks = pInfo[4];
dic.Add(id, p);
}
}
foreach (int i in dic.Keys)
{
product p = dic[i];
Console.WriteLine("Id:{0},name:{1},count:{2},remarks:{3},goodproductNo:{4},badproductNo:{5}", p.id, p.name, p.count, p.remarks, p.goodproductNo, p.badproductNo);
}