-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (132 loc) · 4.96 KB
/
Program.cs
File metadata and controls
151 lines (132 loc) · 4.96 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
string? choice;
string fileName = "courseData.txt";
// Resolve the actual file path so running from bin\Debug or the project root both find the file.
string file = ResolveDataFilePath(fileName);
// Local helper: look for the file in the exe directory and parent folders, then in the current directory and parents.
string ResolveDataFilePath(string name)
{
// 1) Search from the application's base directory (where the exe runs)
string dir = AppContext.BaseDirectory ?? Environment.CurrentDirectory;
for (int i = 0; i < 8; i++)
{
string candidate = Path.Combine(dir, name);
if (File.Exists(candidate)) return candidate;
var parent = Directory.GetParent(dir);
if (parent == null) break;
dir = parent.FullName;
}
// 2) Search from the current working directory
dir = Environment.CurrentDirectory;
for (int i = 0; i < 8; i++)
{
string candidate = Path.Combine(dir, name);
if (File.Exists(candidate)) return candidate;
var parent = Directory.GetParent(dir);
if (parent == null) break;
dir = parent.FullName;
}
// 3) Not found — default to current directory path (file will be created there when writing)
return Path.Combine(Environment.CurrentDirectory, name);
}
do
{
Console.Clear();
Console.WriteLine("1) Read data from file.");
Console.WriteLine("2) Create file from data.");
Console.WriteLine("3) Wah");
Console.WriteLine("Enter any other key to exit.");
choice = Console.ReadLine();
if (choice == "1")
{
Console.Clear();
if (File.Exists(file))
{
using (StreamReader sr = new(file))
{
while (!sr.EndOfStream)
{
string? line = sr.ReadLine();
string[] arr = String.IsNullOrEmpty(line) ? Array.Empty<string>() : line.Split('|');
if (arr.Length >= 3)
{
Console.WriteLine("ID: " + arr[0]);
Console.WriteLine("Name : " + arr[1]);
Console.WriteLine("Realationship to Mario: " + arr[2]);
Console.WriteLine("-----------------------");
}
else if (arr.Length > 0)
{
Console.WriteLine("Malformed line: " + line);
Console.WriteLine("-----------------------");
}
// else empty line -> skip
}
}
Console.WriteLine("Press Enter to continue...");
string? go = Console.ReadLine();
if (go != null) { Console.Clear(); }
}
else
{
Console.Clear();
Console.WriteLine("File does not exist");
}
}
else if (choice == "2")
{
Console.Clear();
// Open the file for appending so existing data is preserved
using (StreamWriter sw = new(file, append: true))
{
int id = 1;
// If file already has entries, set id to next available
if (File.Exists(file))
{
try
{
var lines = File.ReadAllLines(file);
foreach (var l in lines)
{
if (String.IsNullOrWhiteSpace(l)) continue;
var parts = l.Split('|');
if (parts.Length >= 1 && int.TryParse(parts[0], out int parsed))
{
if (parsed >= id) id = parsed + 1;
}
}
}
catch
{
// ignore read errors; start id at 1
id = 1;
}
}
int added = 0;
for (;;)
{
Console.WriteLine("Enter a character? (Y to add, any other key to stop)");
string? resp = Console.ReadLine()?.ToUpper();
if (resp != "Y")
{
break; // enough entries added
}
Console.WriteLine("Enter the characters name.");
string? name = Console.ReadLine();
Console.WriteLine("Enter the characters relationship to Mario.");
string? relationship = Console.ReadLine();
sw.WriteLine($"{id}|{name}|{relationship}");
id++;
added++;
Console.Clear();
}
}
Console.Clear();
} else if (choice == "3")
{
Console.Clear();
for (int i = 0; i < 255; i++){
Thread.Sleep(10);
Console.WriteLine("Waaah!");
}
}
} while (choice == "1" || choice == "2");