gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:29 24 4
gpt4 key购买 nike

我的目标是读取包含坐标数(在第一行中提到)和 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com