6 Replies - 1007 Views - Last Post: 25 November 2013 - 02:49 PM Rate Topic: -----

#1 mrm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-January 13

How do I upload values from a file to a 2 dimensional array?

Posted 24 November 2013 - 05:41 PM

Ok, I have a text file (file.txt) that is comma denominated with 3 sets of values. Each value set is in 13 rows with 12 values in each row.
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0 x 1", "0 x 2", "0 x 3", "0 x 4", "0 x 5", "0 x 6", "0 x 7", "0 x 8", "0 x 9", "0 x 10", "0 x 11", "0 x 12",
"1 x 1", "1 x 2", "1 x 3", "1 x 4", "1 x 5", "1 x 6", "1 x 7", "1 x 8", "1 x 9", "1 x 10", "1 x 11", "1 x 12",
"2 x 1", "2 x 2", "2 x 3", "2 x 4", "2 x 5", "2 x 6", "2 x 7", "2 x 8", "2 x 9", "2 x 10", "2 x 11", "2 x 12",
"3 x 1", "3 x 2", "3 x 3", "3 x 4", "3 x 5", "3 x 6", "3 x 7", "3 x 8", "3 x 9", "3 x 10", "3 x 11", "3 x 12",
"4 x 1", "4 x 2", "4 x 3", "4 x 4", "4 x 5", "4 x 6", "4 x 7", "4 x 8", "4 x 9", "4 x 10", "4 x 11", "4 x 12",
"5 x 1", "5 x 2", "5 x 3", "5 x 4", "5 x 5", "5 x 6", "5 x 7", "5 x 8", "5 x 9", "5 x 10", "5 x 11", "5 x 12",
"6 x 1", "6 x 2", "6 x 3", "6 x 4", "6 x 5", "6 x 6", "6 x 7", "6 x 8", "6 x 9", "6 x 10", "6 x 11", "6 x 12",
"7 x 1", "7 x 2", "7 x 3", "7 x 4", "7 x 5", "7 x 6", "7 x 7", "7 x 8", "7 x 9", "7 x 10", "7 x 11", "7 x 12",
"8 x 1", "8 x 2", "8 x 3", "8 x 4", "8 x 5", "8 x 6", "8 x 7", "8 x 8", "8 x 9", "8 x 10", "8 x 11", "8 x 12",
"9 x 1", "9 x 2", "9 x 3", "9 x 4", "9 x 5", "9 x 6", "9 x 7", "9 x 8", "9 x 9", "9 x 10", "9 x 11", "9 x 12",
"10 x 1", "10 x 2", "10 x 3", "10 x 4", "10 x 5", "10 x 6", "10 x 7", "10 x 8", "10 x 9", "10 x 10", "10 x 11", "10 x 12",
"11 x 1", "11 x 2", "11 x 3", "11 x 4", "11 x 5", "11 x 6", "11 x 7", "11 x 8", "11 x 9", "11 x 10", "11 x 11", "11 x 12",
"12 x 1", "12 x 2", "12 x 3", "12 x 4", "12 x 5", "12 x 6", "12 x 7", "12 x 8", "12 x 9", "12 x 10", "12 x 11", "12 x 12","0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24",
"3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36",
"4", "8", "12", "16", "20", "24", "28", "32", "36", "40", "44", "48",
"5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60",
"6", "12", "18", "24", "30", "36", "42", "48", "54", "60", "66", "72",
"7", "14", "21", "28", "35", "42", "49", "56", "63", "70", "77", "84",
"8", "16", "24", "32", "40", "48", "56", "64", "72", "80", "88", "96",
"9", "18", "27", "36", "45", "54", "63", "72", "81", "90", "99", "108",
"10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "110", "120",
"11", "22", "33", "44", "55", "66", "77", "88", "99", "110", "121", "132",
"12", "24", "36", "48", "60", "72", "84", "96", "108", "120", "132", "144"



And I have a 2 dimensional array with 3 rows and 156 columns in my program.
        string[,] array = new string[3, 156]



What I want to do is read the first 13 rows of the file into the first row of the string array. Than the second set of 13 rows of the file (rows 14 through row 27) into the second row of the string array. And finally the last 13 rows of the file (row 28 through 41) into the third row of the string array.

How would I do this? Or would it be simpler to put them in 3 separate arrays? And if so how would I separate how to read them into each array?

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I upload values from a file to a 2 dimensional array?

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2542
  • View blog
  • Posts: 10,187
  • Joined: 03-December 12

Re: How do I upload values from a file to a 2 dimensional array?

Posted 24 November 2013 - 07:01 PM

Step 1. Do you know how to read the file?

2. create a loop for the entire file.

3. Because you know the exact size,
create another loop that counts to the first set,
second set,
third set.
4. EoF is reached and all values you be in their respective containers.

This post has been edited by astonecipher: 24 November 2013 - 07:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 mrm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-January 13

Re: How do I upload values from a file to a 2 dimensional array?

Posted 25 November 2013 - 10:51 AM

I have not done reading and writing from a file before. But this is what my research turned up. Yet it isn't working. Can you please explain what I have done wrong? Thank you for the help.

        public static void ReadCSV(string strFileName)
        {
              StreamReader sr = new StreamReader(strFileName);
 
              string strline = "";
              string[] _values = null;
            string[,] results = new string[3, 156];

              while (!sr.EndOfStream)
              {
                      for (int i = 0; i < 156; i++)
                      {
                          for (int j = 0; j < 13; j++)
                          {
                              strline = sr.ReadLine();
                              _values = strline.Split(',');
                              results[0, i] = _values[j];
                              
                          }
                          for (int h = 13; h < 25; h++)
                          {
                              strline = sr.ReadLine();
                              _values = strline.Split(',');
                              results[1, i] = _values[h];
                          }
                          for (int g = 25; g < 156; g++)
                          {
                              strline = sr.ReadLine();
                              _values = strline.Split(',');
                              results[2, i] = _values[g];
                          }
                      }
              }//end while
        }//end function


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6220
  • View blog
  • Posts: 21,469
  • Joined: 05-May 12

Re: How do I upload values from a file to a 2 dimensional array?

Posted 25 November 2013 - 11:20 AM

What is the outer i-loop used for?
Was This Post Helpful? 0
  • +
  • -

#5 mrm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-January 13

Re: How do I upload values from a file to a 2 dimensional array?

Posted 25 November 2013 - 12:44 PM

I figured I needed the number of the column for the 2D array. So the outer for loop is for the index of the columns. Is there a better way to do it? Because this code doesn't work.
Was This Post Helpful? 0
  • +
  • -

#6 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: How do I upload values from a file to a 2 dimensional array?

Posted 25 November 2013 - 02:20 PM

Quote

And I have a 2 dimensional array with 3 rows and 156 columns in my program.

What I want to do is read the first 13 rows of the file into the first row of the string array. Than the second set of 13 rows of the file (rows 14 through row 27) into the second row of the string array. And finally the last 13 rows of the file (row 28 through 41) into the third row of the string array.


Don't you mean columns, you already said you have only 3 rows, but you are trying to read first 13 rows. You only have 3 rows.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How do I upload values from a file to a 2 dimensional array?

Posted 25 November 2013 - 02:49 PM

No, he's saying that his file structure doesn't match his array structure. 12 columns in the file, he wants to take thirteen rows of the file and stick them in the first row of the array, 12*13 = 156, which is the width of his array.

Quote

What I want to do is read the first 13 rows of the file into the first row of the string array. Than the second set of 13 rows of the file (rows 14 through row 27) into the second row of the string array. And finally the last 13 rows of the file (row 28 through 41) into the third row of the string array.


For the record, I think OP miscounted on the rows in the file. 13*3 = 39, not 41. And the file he's shown us has 38 rows. So, which is it?

Regardless, the simplest way to read a file when you need to do something line-by-line is to use File.ReadAllLines. That will give you a string[], each entry is one line of your file.

Then you need to loop through each line. If you want to handle each "column" separately, you need to split them. So, in your loop, you can use String.Split. That will give you a string[] with each column value for a particular row.

Then you can just loop through that, inserting each into your multidimensional array.

Another suggestion, since you want to take "chunks" at a time, you can use the following methods from LINQ: Skip and Take.

Example:

var path = @"c:\dev\test.txt";
var lines = File.ReadAllLines(path);

var firstThirteenLines = lines.Take(13);
var secondThirteenLines = lines.Skip(13).Take(13);
var thirdThirteenLines = lines.Skip(13*2).Take(13);


To use that, you have to have using System.Linq;.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1