【问题标题】:C# Read file, split at ; write in arrayC# 读取文件,在 ; 处拆分写入数组
【发布时间】:2021-08-20 18:08:19
【问题描述】:

所以我有一个这样的文件:

1;2;5
1;3;3
1;4;3
1;5;1
1;6;0

我希望每个数字都尽可能容易访问 所以我想到了多维数组

这就是目前的想法:

System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Hisfantor\Desktop\transport.txt");
            

            int count = 0;
            string linee;
            string line;
            string[] extract;

            while ((linee = file.ReadLine()) != null)
            {
                count++;
            }
            textBox1.Text = Convert.ToString(count);

            double[,] destinations = new double[(int)count, 3];

            for (int i = 0; i < count; i++)
            {
                line = file.ReadLine();
                extract = line.Split(';');
                destinations[i, 1] = Convert.ToDouble(extract[0]);
                destinations[i, 2] = Convert.ToDouble(extract[1]);
                destinations[i, 3] = Convert.ToDouble(extract[2]);

                listBox1.Items.Add(destinations[i, 1]);

            } 

            file.Close();

我尝试了不同的东西,但从来没有在列表框中得到任何东西(只是为了测试)

【问题讨论】:

  • 您正在阅读 Stream 以在第一个循环中结束。使用不是double[,] 的东西来存储您的数据(您可以使用Add() 来存储您的数据)。
  • 想知道副驾驶是否可以将标题变成代码。 copilot.github.com

标签: c# arrays file


【解决方案1】:

试试这个:

using System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Hisfantor\Desktop\transport.txt");
        
int count = 0;
string line;
string[] extract;

while ((line = file.ReadLine()) != null)
{
    extract = line.Split(';');
    var lineValue = new LineValue()
    {
        Col1 = Convert.ToDouble(extract[0]),
        Col2 = Convert.ToDouble(extract[1]),
        Col3 = Convert.ToDouble(extract[2]),
    };

    listBox1.Items.Add(lineValue);     
    count++;
}

textBox1.Text = Convert.ToString(count);

public class LineValue
{
    public double Col1 { get; set; }
    public double Col2 { get; set; }
    public double Col3 { get; set; }

    public override string ToString() => $"{Col1};{Col2};{Col3}";
}

【讨论】:

    【解决方案2】:

    我建议 jagged 数组,即数组数组 double[][] 而不是二维数组 (double[,])。然后您可以在 Linq 的帮助下查询文件:

     using System.Linq;
    
     ... 
    
     double[][] destinations = File
       .ReadLines(@"C:\Users\Hisfantor\Desktop\transport.txt")
       .Where(line => !string.IsNullOrWhiteSpace(line))
       .Select(line => line
          .Split(';')
          .Select(item => double.Parse(item))
          .ToArray())
       .ToArray();
    
     foreach (var array in destinations)
       listBox1.Items.Add(array[1]);
    

    【讨论】:

      【解决方案3】:

      这就是我的处理方式。

      class Item
      {
          public int Value1 { get; set; }
          public int Value2 { get; set; }
          public int Value3 { get; set; }
      }
      
      string line;
      List<Item> items = new List<Item>();
      
      using (StreamReader reader = new StreamReader(path))
      {
          while ((line = reader.ReadLine()) != null)
          {
              string[] tokens = line.Split(';');
              if (tokens.Length == 3)
              {
                  int value;
                  Item item = new Item();
                  item.Value1 = int.TryParse(tokens[0], out value) ? value : 0;
                  item.Value2 = int.TryParse(tokens[1], out value) ? value : 0;
                  item.Value3 = int.TryParse(tokens[2], out value) ? value : 0;
                  items.Add(item);
              }
          }
      }
      

      此代码一次读取一行,并在读取时填充Items 的列表。它包括错误检查以避免由于无效输入而导致的意外异常。

      此外,您应该将 StreamReader 包装在 using 块中,以确保及时清理,即使异常提前退出您的方法。

      【讨论】:

        猜你喜欢
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 2016-08-01
        • 1970-01-01
        • 2020-05-15
        相关资源
        最近更新 更多