-3

假设我有这样的文本文件

<pre>----------------

    hPa     m      C 
---------------------
 1004.0     28   13.6
 1000.0     62   16.2
  998.0     79   17.2
  992.0    131   18.0
<pre>----------------
Sometext here

 1000.0     10   10.6
 1000.0     10   11.2
  900.0     10   12.2
  900.0    100   13.0
<aaa>----------------

如何在 C# 中创建数组,从第 5 行(1004.0)读取文本文件到以字符串开头的行之前<pre>-

我使用 string[] lines =System.IO.File.ReadAllLines(Filepath); 来制作数组中的每一行问题是我只想要数组中第一部分的数字,以便稍后将它们分隔到另外 3 个数组 (hPa, m, C) 。

4

2 回答 2

0

你是这个意思吗?

System.IO.StreamReader file = new System.IO.StreamReader(FILE_PATH);

int skipLines = 5;
for (int i = 0; i < skipLines; i++)
{
    file.ReadLine();
}

// Do what you want here.
于 2013-05-06T09:00:22.497 回答
0

这是一个可能的解决方案。它可能比应有的复杂得多,但这应该让您了解进一步完善数据的可能机制。

        string[] lines = System.IO.File.ReadAllLines("test.txt");
        List<double> results = new List<double>();
        foreach (var line in lines.Skip(4))
        {
            if (line.StartsWith("<pre>"))
                break;
            Regex numberReg = new Regex(@"\d+(\.\d){0,1}");  //will find any number ending in ".X" - it's primitive, and won't work for something like 0.01, but no such data showed up in your example
            var result = numberReg.Matches(line).Cast<Match>().FirstOrDefault();  //use only the first number from each line. You could use Cast<Match>().Skip(1).FirstOrDefault to get the second, and so on...
            if (result != null)
                results.Add(Convert.ToDouble(result.Value, System.Globalization.CultureInfo.InvariantCulture));  //Note the use of InvariantCulture, otherwise you may need to worry about , or . in your numbers
        }
于 2013-05-06T09:15:17.453 回答