none
line array RRS feed

  • Question

  • I have a text file that contain for example:

    q 7 8 o, 1 4 p 7, a 4 7 8 

    i 1 4 5, p 1 7 8

    I want to read each line alone and put each one in a different 2d array using c#.

    the result must be as follow:

    first line: 

    q 7 8 o                           

    1 4 p 7        

    a 4 7 8 

    second line:  

    i 1 4 5

    p 1 7 8


    Sunday, April 7, 2013 9:26 AM

Answers

  • Something like below would do it. You will end up with an ArrayList of string arrays produced by string.Split(',').

    static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\test.txt");
    
        ArrayList results = new ArrayList();
    
        foreach (string line in lines)
        {
            results.Add(line.Split(','));
        }
    }

    Sunday, April 7, 2013 11:29 AM
  • The easiest way is to add to a List<> object.  The List can then be accessed exactly like an array.  the code worked the 1st time I ran it and then "while (reader != null)" stopped working so I had to make some changes to prevent an exception.

    class Program
        {
            static List<List<string>> myArray = new List<List<string>>();
            static void Main(string[] args)
            {
                
                TextReader reader = File.OpenText(@"c:\temp\tmp.txt");
                string inputline = "";
                while (inputline != null)
                {
                     inputline = reader.ReadLine();
                     if(inputline != null)
                     {
                        string[] splitArray = inputline.Split(',');
                        myArray.Add(splitArray.ToList());
                     }
                }
                 string cell = myArray[1][0];
                 reader.Close();
                 reader.Dispose();
            }
        }


    jdweng

    Sunday, April 7, 2013 12:41 PM

All replies

  • Something like below would do it. You will end up with an ArrayList of string arrays produced by string.Split(',').

    static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\test.txt");
    
        ArrayList results = new ArrayList();
    
        foreach (string line in lines)
        {
            results.Add(line.Split(','));
        }
    }

    Sunday, April 7, 2013 11:29 AM
  • The easiest way is to add to a List<> object.  The List can then be accessed exactly like an array.  the code worked the 1st time I ran it and then "while (reader != null)" stopped working so I had to make some changes to prevent an exception.

    class Program
        {
            static List<List<string>> myArray = new List<List<string>>();
            static void Main(string[] args)
            {
                
                TextReader reader = File.OpenText(@"c:\temp\tmp.txt");
                string inputline = "";
                while (inputline != null)
                {
                     inputline = reader.ReadLine();
                     if(inputline != null)
                     {
                        string[] splitArray = inputline.Split(',');
                        myArray.Add(splitArray.ToList());
                     }
                }
                 string cell = myArray[1][0];
                 reader.Close();
                 reader.Dispose();
            }
        }


    jdweng

    Sunday, April 7, 2013 12:41 PM