Reputation: 41
I have problem in reading 2D text from file and import it to an int array. Specifically, my text file looks like below:
2,3,4,5,6
5,2,3,4,5
2,4,6,7,4
2,7,8,5,6
So each cell in matrix is separated by comma and each new row starts with new line.
I tried many ways to make it works but I can't! Simply, I want an int[][]
or int[,]
array at the end.
P.S: I can read 1-D matrix simply to int[]
as below:
int[] array= File.ReadAllText(fileppath).Split(',').Select(t => int.Parse(t)).ToArray();
Upvotes: 1
Views: 3679
Reputation: 4119
Just to illustrate the point, the problem, that you have is storing the references of each line into a jagged array int[][] or into a bi-dimensional array int[,]
I suggest you to take a look here before https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396
If you want to create a jagged array is simple, the first dimension will be the amount of lines that you have on your file. Remember this jagged array has arrays in each positions
int[][] a = new int[amountOfLinesTxt][];
because you can get the array per line, the only thing that you need to do is to assign the array to the specific position for example
int[] array= File.ReadAllText(fileppath).Split(',').Select(t => int.Parse(t)).ToArray();
a[0] = array
Same thing for the other lines.
Now if you want to use a bi-dimensional array you need to specify in your case from the begging the dimensions, for example int[,] a = new int[amountOfRowsTxt, amountofColumnsTxt];
then while you are reading your lines you need to save the items inside of it.
int[] array= File.ReadAllText(fileppath).Split(',').Select(t => int.Parse(t)).ToArray();
for(int i = 0; array.Length; i ++)
{
a[currentRow, i] = array[i];
}
Got the idea? Of course there are better ways to get the result using Linq, just take a look on the previous answers. Hope this helps
Upvotes: 0
Reputation: 2466
You first need to add the index of each line to the first dimension and then every 'column 'in the line to the second dimension.
Try the following code:
int[][] array = File.ReadAllText(filepath).Split('\n')
.Select(r => (r.Split(','))
.Select(c => int.Parse(c)).ToArray()).ToArray();
Upvotes: 1
Reputation: 716
Try this:
String input = File.ReadAllText( @"c:\myfile.txt" );
int i = 0, j = 0;
int[,] res = new int[10, 10];
foreach (var row in input.Split('\n'))
{
j = 0;
foreach (var col in row.Trim().Split(' '))
{
res[i, j] = int.Parse(col.Trim());
j++;
}
i++;
}
If that dint work you also have an alternative:
int[][] list = File.ReadAllLines("myfile.txt")
.Select(l => l.Split(',').Select(i => int.Parse(i)).ToArray())
.ToArray();
Upvotes: 1
Reputation: 153
You are reading all lines, but the code then only processes one line. If you're trying to make a 2D array of integers, you want something like this:
string text = @"2,3,4,5,6 5,2,3,4,5 2,4,6,7,4";
text.Split('\n').Select(line => line.Split(',').Select(t => int.Parse(t)).ToArray()).ToArray();
(where for test purposes I've replaced the file read with a static string)
Upvotes: 0
Reputation: 8194
// Read the text file
var text = File.ReadAllLines(@"path\to\file.txt");
// Split on `,`, convert to int32, add to array, add to outer array
var result = text.Select(x => (x.Split(',').Select(Int32.Parse).ToArray())).ToArray();
Result is int[][]
.
Upvotes: 1