Es87 Es87 - 1 month ago 19
C# Question

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

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

Answer

I've changed your double-linked list to a plain List<cars> for simplicity. You need to create a new cars item for each record, otherwise you'll end up with every element in your list referring to the same record (the last one processed). Each line has a different attribute of the car so you need to work out which attribute to save, using a counter (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) );
        }
    }