将文本文件中的数据读取到具有不同数据类型的结构中 C#

Reading data from a text file into a struct with different data types c#

我有一个存储有关汽车数据的文本文件,其名称为 car.txt。我想阅读此文本文件并对每辆车的每个部分进行分类。最终我想将此信息添加到双向链表中。到目前为止,我所尝试的一切要么给我格式异常,要么给我超出范围的异常。 这是我的 public 结构

public struct cars
    {
        public int id;
        public string Make;
        public string Model;
        public double Year;
        public double Mileage;
        public double Price;
    }

这是我尝试将数据读入结构然后将其添加到 DLL 的地方

static void Main()
        {
            int Key;
            cars item = new cars();
            int preKey;
            /* create an empty double linked list */
            DoubleLinkedList DLL = new DoubleLinkedList();
            string line;
            StreamReader file = new StreamReader(@"C:\Users\Esther\Desktop\cars.txt");
            while ((line = file.ReadLine()) != null)
            {
                Console.WriteLine(line);
                var array = line.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                item.Make =array[0];
                item.Model = array[1];
                item.Year = Double.Parse(array[2]);
                item.Mileage = Double.Parse(array[2]);
                item.Price = Double.Parse(array[2]);
                // Using newline to seprate each line of the file.
                Console.WriteLine(line);
            DLL.AppendToHead(item);

            }

这是抛出一个格式异常,然后是一个超出范围的异常。如何获取此代码以将文本文件读入结构并将其添加到我的 DLL 中?这是 car.txt 文件

 BMW
 228i
 2008
 122510
 7800

 Honda
 Accord
 2011
 93200
 9850

 Toyota
 Camry
 2010
 85300
 9500

您正在使用 ReadLine 逐行读取文件,并试图拆分带有行尾的单行。这将导致一个只有一个元素的数组(例如 "BMW")

您可以读取整个文件然后按行拆分,或者假设每一行都包含一些数据。

OutOfRangeException 是指当数组仅包含 array[0] = "BMW".

时调用 array[1]

为简单起见,我已将您的双链表更改为普通的 List<cars>。您需要为每条记录创建一个新的 cars 项,否则您最终会得到列表中的每个元素都引用同一条记录(最后处理的记录)。每条线都有不同的汽车属性,因此您需要使用计数器 (idx) 计算出要保存的属性。

    using System.Collections.Generic; // for List<cars>
    ... ... ...
    public static void ReadMultiline()
    {
        // 
        var DLL = new List<cars>();
        string line;
        StreamReader file = new StreamReader(@"cars.txt");
        var item = new cars();
        int idx = 0;
        while ((line = file.ReadLine()) != null)
        {
            idx++;
            switch ( idx ) {
                case 1: item.Make = line; break;
                case 2: item.Model = line; break;
                case 3: item.Year = Double.Parse(line); break;
                case 4: item.Mileage = Double.Parse(line); break;
                case 5: item.Price = Double.Parse(line); break;
            }
            if (line == "" && idx > 0 )
            {
                DLL.Add(item);
                idx = 0;
                item = new cars();  // create new cars item
            }
        }
        // pick up the last one if not saved
        if (idx > 0 )
            DLL.Add(item);

        foreach( var x in DLL )
        {
            Console.WriteLine( String.Format( "Make={0} Model={1} Year={2} Mileage={3} Price={4}", x.Make, x.Model, x.Year, x.Mileage, x.Price) );
        }
    }