Input text file to an array

I am attempting to read a file in, and throw the contents into a matrix, where each element is stored in a different container
Example
Text File
123
456
789

Matrix
{{1,2,3}
 {4,5,6}
 {7,8,9}}

I am using stream reader to read the contents in, and know how to pull the contents into an array, but not a Matrix
LVL 1
cotj73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
Matrix is just a multi-dimensional array. If you could pull the contents into an array then same logic could be used to pull it into a multi-dimensional array.

If you still have an issue please elaborate.
0
Vel EousResearch & Development ManagerCommented:
If you are referring how to convert a string to an array:


string str = "abcde123";
char[] charArray = s.ToCharArray();
0
cotj73Author Commented:
I figured it out, kinda tricky hope someone else will find this usefull
string[][] CharMap;
            StreamReader rdr = new StreamReader("MAP.txt");
            string sTemp = rdr.ReadToEnd();
            //char[] cTemp = sTemp.Split('\n');
            sTemp.Split('\n');
            string[] saTemp = sTemp.Split('\n');
            
            for(int i = 0;i<saTemp.Length;i++)
            {
                saTemp[i] = saTemp[i].Replace('r',' ');
            }
            CharMap = new String[saTemp.Length][];
            
            for(int i = 0;i<saTemp.Length;i++)
            {
                CharMap[i] = saTemp[i].Split(' ');
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anarki_jimbelSenior DeveloperCommented:
Honestly, I don't quite like your solution. One problem I can see is in the last statement:

CharMap[i] = saTemp[i].Split(' ');

In your file
123
456
789
Digits are NOT separated with spaces - so it won't work.
So the solution does not satisfy your requirements.

I can suggest another solution. It's not shorter really:
        private void button1_Click(object sender, EventArgs e)
        {
                string[][] CharMap;
                List<string> lines = new List<string>();
                using (StreamReader sr = new StreamReader("text.txt"))
                {
                    String line;
                    // Read and display lines from the file until the end of the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        lines.Add(line);
                    }
                }
                CharMap = new String[lines.Count][];
                for (int firstIndex = 0; firstIndex < lines.Count; firstIndex++)
                {
                    CharMap[firstIndex] = new string[lines[firstIndex].Length];
                    for (int secondIndex = 0; secondIndex < lines[firstIndex].Length; secondIndex++)
                    {
                        CharMap[firstIndex][secondIndex] = lines[firstIndex].Substring(secondIndex, 1);
                    }
                }
                // display result in output window
                for (int firstIndex = 0; firstIndex < CharMap.Length; firstIndex++)
                {
                    for (int secondIndex = 0; secondIndex < CharMap[firstIndex].Length; secondIndex++)
                    {
                        System.Diagnostics.Debug.Write(CharMap[firstIndex][secondIndex]);
                    }
                    System.Diagnostics.Debug.WriteLine("");
                }
        }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.