I have been using this link as an example, but have been having troubles with it: 2d Array from text file c#
I have a textfile that contains :
1 1 0 0
1 1 1 0
0 0 0 1
0 1 0 0
And I'm trying to use the function:
static void Training_Pattern_Coords()
{
String input = File.ReadAllText(@"C:\Left.txt");
int i = 0, j = 0;
int[,] result = new int[4, 4];
foreach (var row in input.Split('\n'))
{
j = 0;
foreach (var col in row.Trim().Split(' '))
{
result[i, j] = int.Parse(col.Trim());
j++;
}
i++;
}
Console.WriteLine(result[1, 3]);
Console.ReadLine();
}
However I keep getting the error message (Input String was not in correct format) at the line :
foreach (var row in input.Split('\n'))
I think it has something to do with the spaces within the textfile but I'm not entirely sure. Thanks for your help!