Hello all,

i have a text file that contain data like this:

1 0.3232 0.361 0.5214 0.233 -0.7678
2 0.3451 0.321 0.134 0.224 -0.706268
3 0.3123 0.351 0.155 0.523 -0.70626
4 0.36 0.312 0.216 0.233 -0.6453351
5 0.269 0.3331 0.162 0.224 -0.584962

i did this using

string[] data = File.ReadAllLines("test.txt");

so what i want is to create an int array of each line of this text file and then use this array in other mathematical procedures

how this can be done ????


Edited by weblover: n/a

5 Years
Discussion Span
Last Post by ddanbe

Your text file has more the looks of floating point numbers.
Please explain a bit more what you really want.


Each line has multiple numbers in it, of which only the first is an int. I think what you really want is a two dimensional array of type double.

Something like the following (haven't tested in any way shape or form, treat this as sudo code)

I hope this is what you are looking for. If not, can you be more clear as to what exactly you need.

string[] data = File.ReadAllLines("test.txt");
double[,] numbers = new double[data.length, 6]

for(int x = 0; x < data.length; x++)
    string[] temp = data[x].split(' ');

    //assuming the first result is only used for being an index
    for(int y = 1; y < temp.length; y++)
        double[x][y] = Convert.ToDouble(temp[y]);

sorry ddanbe , youre right , all the data are float , so it dosent matter i just want to place each row of the file in an array and use the values for some calculation.

stbuchok the first line is just an index , so it's not so important.

and btw the text file is so big , not only 6 lines

Edited by weblover: n/a


the 6 is for each number of the line, not the number of rows. You'll notice that I split each line by the space, so first time it goes through, you will get the following:
(I just realized for the inner loop I have double[x][y], this should be numbers[x][y])

numbers[0][0] = 1 
numbers[0][1] = 0.3232
numbers[0][2] = 0.361
numbers[0][3] = 0.5214
numbers[0][4] = 0.233
numbers[0][5] = -0.7678

Basically the code in the first response is saying this.

1. Create a 2 dimensional array where the first array will be a length of how many rows are returned and the second array will be the number of columns in each line.
2. Loop through each row and create an array that is made up of each row being split by the space between the numbers
3. For each of those arrays, go through each and convert them to a double and put them into the array of doubles.

Please read through the code.


i made some changes and i'm testing now

Edited by weblover: n/a


i found a small error , it's giving me this error : Error:Wrong number of indices inside []; expected '2'

so i changed numbers[x][y] to numbers [x,y] and it worked.

will this make any problems ???


it works as needed , but still have an error : Error:Operator '^' cannot be applied to operands of type 'double' and 'int'

the error is on this part of the code :

error=error + ((bb-ol)*(alp) + d -(numbers[bb,5]))^2;

why ???

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.