Tak na szybko, bo nie chciało mi się rozbijać tego na funkcje:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _4programmers_CSharp2 {
class Program {
static void Main(string[] args) {
FileStream plik = null;
try {
plik = new FileStream("macierz.txt", FileMode.Open, FileAccess.Read);
string linijka = null;
List<string> wiersze = new List<string>();
StreamReader odczyt = new StreamReader(plik);
while ((linijka = odczyt.ReadLine()) != null)
wiersze.Add(linijka);
int[][] tab = new int[wiersze.Count][];
string[] split = null;
for (int i = 0; i < wiersze.Count; i++) {
split = wiersze[i].Split(new char[] { ' ' }, StringSplitOptions.None);
tab[i] = new int[split.Length];
for (int j = 0; j < tab[i].Length; j++)
tab[i][j] = Int32.Parse(split[j]);
}
for (int i = 0; i < tab.Length; i++) {
for (int j = 0; j < tab[i].Length; j++)
Console.Write(tab[i][j] + " ");
Console.WriteLine();
}
Console.ReadKey();
}
catch (IOException e) {
Console.WriteLine(e.ToString());
Console.ReadKey();
}
finally {
if (plik != null)
plik.Close();
}
}
}
}
Plik ma postać:
1 5 3 2 1 4 1
3 2 5 4 3 4 1
3 5 2 5 1 2 0
2 3 1 5 5 6 1
Ścieżkę do pliku pobierzesz używając:
dialog.FileName.ToString();
[EDIT]:
Kurcze już piąty raz poprawiam po jednym znaku. Trzeba iść spać...