Hien Tran
Hien Tran

Reputation: 1493

Read a text file and add to a list

I am having a text file like this. I'd like to read line by line and store these information into a List<Store>, with Store is a custom types.

Store ID: 01
Name: Kingfisher
Branch Number: 1
Address: 23 Emerson St
Phone: 422361609
-----END OF RECORD-----

Store ID: 02
Name: Harvey
Branch Number: 2
Address: 23 Korby St
Phone: 422361609
-----END OF RECORD-----

Store ID: 175
Name: Roger
Branch Number: 4
Address: 275 Carmody Rd
Phone: 428395719
-----END OF RECORD-----

Here is what I am using. Because the record is always with the order shown in the list, so I read line by line and assign the store.ID, store.name,... until it reaches the final attribute of a record is store.phoneNumber, it will then add it to the List and keep going in the foreach loop when it comes to the next Store ID.

But the problem is when I do search in the list, it can only return the final record. It seems like the List only has one element, which is the final record. Can anyone please point out where I am wrong?

    var storeTemp = new Store();
    List<Store> stores = new List<Store>();
    foreach (string line in File.ReadAllLines(@"C:\Store.txt"))
    {
        if (line.Contains("Store ID: "))
            storeTemp.ID = line.Substring(10);
        if (line.Contains("Name: "))
            storeTemp.name = line.Substring(6);
        if (line.Contains("Branch Number: "))
            storeTemp.branchNO = Convert.ToInt32(line.Substring(15));
        if (line.Contains("Address: "))
            storeTemp.address = line.Substring(9);
        if (line.Contains("Phone: "))
        {
            storeTemp.phoneNumber = Convert.ToInt32(line.Substring(7));
            stores.Add(storeTemp);
        }
    }

Upvotes: 0

Views: 245

Answers (4)

Nitish Tripathi
Nitish Tripathi

Reputation: 11

You much create a new space in memory for storeTemp everytime . Currently you are over writing the data in the same location .

Upvotes: 0

Mehdi Khademloo
Mehdi Khademloo

Reputation: 2812

Just change the type of Store from Class to Struct

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

I would use LINQ instead and the Batch method to create batches of lines so you can set the properties easily:

File.ReadLines("path")
.Batch(5)
.Select(x => x.ToList())
.Select(values => new Store 
                  {
                     ID = values[0].Split(':').Trim(),
                     name = values[1].Split(':').Trim(),
                     branchNo = int.Parse(values[2].Split(':').Trim()),
                     address = values[3].Split(':').Trim(),
                     phoneNumber =int.Parse(values[4].Split(':').Trim())
                  }.ToList();

You need add reference to MoreLINQ library in order to use Batch method.

Upvotes: 4

Max
Max

Reputation: 7080

        List<Store> stores = new List<Store>();
            var storeTemp = new Store();

        foreach (string line in File.ReadAllLines(@"C:\Store.txt"))
        {
            // You needto create a new instance each time
            if (line.Contains("Store ID: "))
                storeTemp.ID = line.Substring(10);
            if (line.Contains("Name: "))
                storeTemp.name = line.Substring(6);
            if (line.Contains("Branch Number: "))
                storeTemp.branchNO = Convert.ToInt32(line.Substring(15));
            if (line.Contains("Address: "))
                storeTemp.address = line.Substring(9);
            if (line.Contains("Phone: "))
            {
                storeTemp.phoneNumber = Convert.ToInt32(line.Substring(7));
                stores.Add(storeTemp);
                storeTemp = new Store(); // You need to recreate the object, otherwise you overwrite same instance

            }
        }

Upvotes: 1

Related Questions