从text读内容存到二维数组使用的问题

right89757 2012-10-23 05:10:17
点 纬度 经度 海拔
0, 40.123, -1.231, 10.59
1, 40.230, -1.242, 10.51
2, 40.235, -1.2425, 0

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

暂时只写成这样。。

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(',');

}

}
...全文
142 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
right89757 2012-10-23
  • 打赏
  • 举报
回复
其实那个行末是没有comma的,我写错了。。不过还是感谢,顺便可以学习一下有comaa的写法[Quote=引用 5 楼 的回复:]

C# code
string[] rows = File.ReadAllLines(@"E:\nottingham\study\PRG\Projects\distance_P2P\distance_P2P\Coordinate.txt");
string[][] values = new string[rows.Length][];
for (i……
[/Quote]
right89757 2012-10-23
  • 打赏
  • 举报
回复
刚开始写的时候就是看了您的帖,每想到又来这里回我。真是太谢谢了~我先研究一下这段[Quote=引用 6 楼 的回复:]

string[] temp = System.IO.File.ReadAllLines("E:\\aaa.txt", System.Text.Encoding.GetEncoding("gb2312"));
Dictionary<int, product> dic = new Dictionary<int, product>();
forea……
[/Quote]
蝶恋花雨 2012-10-23
  • 打赏
  • 举报
回复
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);
}
dalmeeme 2012-10-23
  • 打赏
  • 举报
回复
		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();
}
wuyq11 2012-10-23
  • 打赏
  • 举报
回复
foreach(string s in File.ReadAllLines(""))
{
string[] arr=s.Split(',');

}
right89757 2012-10-23
  • 打赏
  • 举报
回复
怎么split成那个二维数组的格式[Quote=引用 1 楼 的回复:]

File.ReadAllLines("")
再split分割
[/Quote]
right89757 2012-10-23
  • 打赏
  • 举报
回复
是先把所有行读进来么?
split分割是以数据为单位还是整行?[Quote=引用 1 楼 的回复:]

File.ReadAllLines("")
再split分割
[/Quote]
wuyq11 2012-10-23
  • 打赏
  • 举报
回复
File.ReadAllLines("")
再split分割

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧