6
Reply

reading a text file, line by line and put it in 3D array in C#...I got Problem...

yamid

yamid

Nov 6 2008 4:12 AM
2.5k

 

I tried the solution Alan kindly provided to me( find the question and reply, below) , but every time there is an error say , the argument is not in correct format...

would you please tell me about?
To simplify my question, please look at below simplified example, ( you can find the previous question and codes, below section)

you can think of the 16 data like this:
I would like to put them in a 3d array with
4*2*2 dimension.

data file:
1 2 3 4 5 6 7 8
9 10 11 12 13
14 15 16

so,
for k=0 I have ,
1 2 3 4
5 6 7 8
and for k=1 I have
9 10 11 12
13 14 15 16

for example A[0,0,0]=1
A[1,0,0]=2
A[2,0,0]=3
A[3,0,0]=4
A[0,1,0]=5
A[1,1,0]=6
A[2,1,0]=7
A[3,1,0]=8
A[0,0,1]=9
A[1,0,1]=10
A[2,0,1]=11
A[3,0,1]=12
A[0,1,1]=13
A[1,1,1]=14
A[2,1,1]=15
A[3,1,1]=16


--PREVIOUS Question-----------------------------------------------------------------------------------------------------------------------------------------

I have a text data file including Double type data ,  as below;
x x x x
x x x x
.
.
.
x x

How to read this data file line-by line from left to right and put them in a 3D array(in C#). The order of numbers in data file  is in the way that first i changes from 0-75, then j to 139 and then k to149.
it means the
filling procedure is like this

reading to array from (0,0,0)  to (75,0,0) then (0,1,0) to (75,1,0)...   then(0,139,0)to(75,139,0)
(0,0,1) to(75,0,1) then(0,1,1)to(75,1,1)...    then(0,139,1)to(75,139,1)
.
.
.
(0,0,149)to(75,0,149) then (0,1,149)to(75,1,149) ...then(0,139,149)to(75,139,149)

 

 ------Previous solution( cause error)-----------------------------------------------------------------------------------------------------------------------------------------------------


double[,,] dArray = new double[76,140,150];
string line = null;
string[] numbers = null;

StringReader sr = new StringReader(filePath);

for (int k = 0; k < 149; k++)
{
   for(int j = 0; j < 139; j++)
   {
      line = sr.ReadLine();
      numbers = line.Split(',');
      for(int i = 0; i < 75; i++)
      {
         dArray[i,j,k] = double.Parse(numbers[i]);
      }
   }
}

sr.Close();


Answers (6)