Hey all. At the moment i'm struggling to read a file which contains 4 integers on each line separated by commas. I want to read each line then put them in a list like
Text File will look like
1, 3, 7, 9
2, 5, 6, 8

I want it to be put in a list. Basically so that in the list it contains 1 3 7 and 9, the next part of the list contains 2 5 6 and 8 etc.
The code i have for it is

public class NewList
{     
        public int ID { get; set; }
        public int N1 { get; set; }
        public int N2 { get; set; }
        public int N3 { get; set; }
        public NewList(int pID, int pN1, int pN2, int pN3)
        {
            this.ID = pID;
            this.N1 = pN1;
            this.N2 = pN2;
            this.N3 = pN3;
        }

Console.WriteLine("");
FileStream aFile = new FileStream("SomeData.txt", FileMode.Open);
List<NewList> Numbers = new List<>(NewList);

//In the Main()
using(StreamReader sr = new StreamReader(aFile))
{
   while((strLine = sr.ReadLine()) != null)
   {
    Numbers.Add(strLine);
    }
}

I know i need to involve Split somewhere as well

Edited 5 Years Ago by Drakarus: n/a

This should do it:
But I am not sure what do you want exactly. Or do you want to put every single number from the file into its own row in the List (like: 1,3,7,9 add evey number to the list seperately), or you want to "join" number from one row (like : 1,3,7,9 join into 1379 and add this number to the list)?

This is the code:

FileStream aFile = new FileStream("SomeData.txt", FileMode.Open);
            List<NewList> Numbers = new List<NewList>();
            using (StreamReader sr = new StreamReader(aFile))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    //1.Putting every number into its own row in the list<T>:
                    string[] array = line.Split(',');
                    for (int i = 0; i < array.Length; i++)
                        Numbers.Add(array[i].ToString()); //you can remove ToString() method if not needed

                    //2.Putting all number into one row (removing commas from original):
                    if (line.Contains(','))
                    {
                        line = line.Replace(",", "");
                        Numbers.Add(line);
                    }
                }
            }

If this is not it, please exaplin a bit better what exactly would you like to do with the numbers, ok?
Mitja

I want it so i can get the array/list to hold each number so that
1 3 7 9 is in the first part of the array but i can obtain each number separately later. Like (1,3,7,9)(2,5,6,8) etc
So it means keeping the numbers separately apart but that 3, 7 and 9 are still relevant to the number 1. Hope I explained it a bit better.

I have already showed how to do this:

//use the full upper code:
                    if (line.Contains(','))
                    {
                        line = line.Replace(",", "");
                        Numbers.Add(line);
                    }

Will this allow me to manipulate each number afterwards though?
Say in the first row of 1 3 7 9.
Can i access 3 from it?

My question: do you want to have numbers seperated with a whitespace, or together?

Your question: Depends how do you want to.
If you know what the 3 is on 2nd place from the right, you can access to it like:

string str1 = "1357";
            string str2 = "1 3 5 7";
            string a1 = str1.Substring(1,1);
            string a2 = str2.Substring(2, 1);

I have solved this now by using objects. Each line would be split and each number would be stored in a variable. I then display it by using a foreach loop.
Thanks for the help Mitja.

public void TextFileToList(string path)
{
    List<string> exampleList = new List<string>();

    if (File.Exists(path))
    {
        foreach (string line in File.ReadAllLines(path))
        {
            exampleList.Add(line);
        }
    }
}
This question has already been answered. Start a new discussion instead.