locked
Read matrix from txt file RRS feed

  • Question

  • Hi there.

    I want to read from a txt file just one value at time and assign this value to an array. The data has the following form:

    12 34 56

    11 23 34

    2 190 43

     How can I do it?

    I would really appreciate any help of you. Thank you in advance..

    NM

    Tuesday, March 6, 2007 2:54 PM

Answers

  • Hi, smalamas

    If the text has format to follow, it can be done.

    Read the text in txt file into string by streamreader, then put the string into the array.

    If you have further problem, feel free to let me know.

    Thank you

    Wednesday, March 7, 2007 3:46 AM

All replies

  • Will it always be 3x3?
    Tuesday, March 6, 2007 10:48 PM
  • Hi, smalamas

    If the text has format to follow, it can be done.

    Read the text in txt file into string by streamreader, then put the string into the array.

    If you have further problem, feel free to let me know.

    Thank you

    Wednesday, March 7, 2007 3:46 AM
  • Thank you for your replies.

    No, the matrix is not a 3x3, is bigger, but always has a square form.

    The text follows a standard format. I would really appreciate if you could send me an example code.

    I would really appreciate any help of you. Thank you in advance...

    Thursday, March 8, 2007 1:27 PM
  • Hi,

    You can try to treat each line of the text as string and pick the numbers out.

    for example:

                FileStream f = File.OpenRead(@"D:\Matrix.txt");
                StreamReader reader = new StreamReader(f);
                string str = reader.ReadLine();
                while (str != null)
                {
                    //treat each line...
                    str = reader.ReadLine();
                }
                reader.Dispose();

    So what left to you is just to pick some numbers out of a string.

    Thank  you

    Friday, March 9, 2007 2:04 AM