I have a file file.txt with the following:
6,73,6,71
32,1,0,12
3,11,1,134
43,15,43,6
55,0,4,12
And this code to read it and feed it to a jagged array:
string[][] arr =new string[5][];
string[] filelines= File.ReadAllLines("file.txt");
for (int i = 0; i< filelines.Length; i++)
{
arr[i] =filelines[i].Split(',').ToArray();
}
How would I do the same thing, but with a 2D array?
Pravesh Singh
17-Dec-2013Hi Royce,
Assuming you know the dimensions of your 2D array (or at least the maximum dimensions) before you start reading the file, you can do something like this:
string[,] arr = new string[5,4];
If you don't know the dimensions, or if the number of integers on each line may vary, your current code will work, and you can use a little Linq to convert the array after you've read it all in:
Thanks