c# - 将坐标从txt读取到数组中(c#)

标签 c# arrays coordinates readline

我的目标是读取包含坐标数(第一行中提到的)和 X Y 值( double )的 txt 文件。例如:

2

4.454 3.24

1.23 4

我需要将这些坐标放入数组中(没有第一行) 到目前为止,我成功读取了文本,但不知道如何放入数组中。 我的数组是一个 Point (double x,double y) C'tor,但我想学习如何放入普通数组中。 另一个问题是我如何控制我要读哪一行?

到目前为止的代码

       using (StreamReader sr = File.OpenText(fileName)) 
        {
            int i = 0;
            string inputLine;
            int len = int.Parse(inputLine = sr.ReadLine());
            string[] readText = File.ReadAllLines(fileName);
            foreach (string line in File.ReadAllLines(fileName))
            {
                string[] parts = line.Split(' ');
                foreach (string part in parts)
                {
                    Console.WriteLine("{0}:{1}",i, part);    
                }
                i++;
            }
        }

谢谢大家! p.s我的第一个问题,希望不要违反论坛规则。

最佳答案

类似这样的吗?

var points = File.ReadLines("c:\filepath")
    .Skip(1) //Ignore the 1st line
    .Select(line => line.Split(' ')) //Chop the string into x & y
    .Select(split => new Point(double.Parse(split[0]), double.Parse(split[1])); //create a point from the array

关于c# - 将坐标从txt读取到数组中(c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13880220/

相关文章:

c# - 尝试使用 LINQ 按创建日期获取目录中的文件列表

javascript - 对象数组中属性值的递归数组

node.js - MongoDB 架构 : default coordinates for GPS position

java - 坐标 - 查看给定的 X、Y 是否在正方形区域内,如果不在,则找到最近的位置

c# - 启动 wxWidgets C++ 需要轻轻一推

c# - 在c#中显示类似excel的数据

c# - 需要从字符串拆分中将两个值放入数组中

python - PyQT - 如何获取图像选择的坐标

C#窗体绘制题

java - 大于 N(重温)