class FileWork public static Stream BrowseFile OpenFileDialog new Open

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class FileWork
{
public static Stream BrowseFile()
{
OpenFileDialog f = new OpenFileDialog();
Stream fs = null;
f.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
f.Filter = "txt files (*.txt)|*.txt";
if(f.ShowDialog() == DialogResult.OK)
fs = f.OpenFile();
return fs;
}
public static string ReadFile(Stream fileStream)
{
fileStream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(fileStream);
return reader.ReadToEnd();
}
public static int[,] SaveMatrix(Stream fileStream, int N, int M)
{
string file = ReadFile(fileStream);
string[] fileArr;
file = file.Remove(0, 5);
fileArr = file.Split(new Char[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
int len = N * M;
int[,] matrix = new int[len, len];
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
matrix[i, j] = (int)Char.GetNumericValue(fileArr[i][j]);
return matrix;
}
public static void GetSize(Stream fileStream, out int N, out int M)
{
string file = ReadFile(fileStream);
string firstNum = "";
string secondNum = "";
int i = 0;
while (!file[i].Equals('\r'))
{
while (!file[i].Equals(' '))
{
firstNum += file[i];
i++;
}
i++;
while (!file[i].Equals('\r'))
{
secondNum += file[i];
i++;
}
break;
}
N = int.Parse(firstNum);
M = int.Parse(secondNum);
}
}