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

时间:2016-10-09 15:53:29

标签: c# struct streamreader doubly-linked-list

I have a text file that stores data about cars, its call car.txt. I want to read this text file and categorize each of the sections for each car. Ultimately I want to add this information into a doubly linked list. So far everything I have tried is either giving me format exception or out of range exception. This is my public structure

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

This is where I try reading the data into the struct then add it to the 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);

            }

This is throws a format exception then an out of range exception. How do I get this code to read the text file into the struct and add it to my DLL? This is the car.txt file

 BMW
 228i
 2008
 122510
 7800

 Honda
 Accord
 2011
 93200
 9850

 Toyota
 Camry
 2010
 85300
 9500

2 个答案:

答案 0 :(得分:0)

您正在逐行使用ReadLine读取文件,并尝试使用行结尾分割单行。这将导致阵列只有一个元素(E.G。“BMW”)

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

OutOfRangeException是指当数组仅包含array[1]时调用array[0] = "BMW"

答案 1 :(得分:0)

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

    using System.Collections.Generic; // for List<cars>
    ... ... ...
    public static void ReadMultiline()
    {
        // http://stackoverflow.com/questions/39945520/reading-data-from-a-text-file-into-a-struct-with-different-data-types-c-sharp
        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) );
        }
    }