Skip to content

Commit 2871697

Browse files
committed
Adding file problem from final exam.
1 parent 8226089 commit 2871697

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.IO;
3+
4+
class Program
5+
{
6+
static void Main(string[] args)
7+
{
8+
Console.WriteLine("Enter a path");
9+
string path = Console.ReadLine();
10+
if (File.Exists(path))
11+
{
12+
Console.WriteLine(
13+
"Now displaying file at " + path + "."
14+
);
15+
try
16+
{
17+
StreamReader sr = new StreamReader(path);
18+
string cLine = sr.ReadLine();
19+
while (cLine != null)
20+
{
21+
Console.WriteLine(cLine);
22+
cLine = sr.ReadLine();
23+
}
24+
sr.Close();
25+
Console.WriteLine("Done displaying file.");
26+
}
27+
catch
28+
{
29+
Console.WriteLine(
30+
"There was an error opening your file."
31+
);
32+
}
33+
}
34+
else
35+
{
36+
Console.WriteLine(
37+
"Now creating a file at "
38+
+ path
39+
+ ".\n Enter your text, one line at a time. When done, type \"!DONE!\" (without the quotes), then enter."
40+
);
41+
try
42+
{
43+
StreamWriter sw = new StreamWriter(path);
44+
string uInput = Console.ReadLine();
45+
while (uInput != "!DONE!")
46+
{
47+
sw.WriteLine(uInput);
48+
uInput = Console.ReadLine();
49+
}
50+
sw.Close();
51+
Console.WriteLine("File correctly written.");
52+
}
53+
catch
54+
{
55+
Console.WriteLine(
56+
"There was an error creating your file."
57+
);
58+
}
59+
}
60+
}
61+
}

source/solutions/io/files.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,46 @@ tags:
138138
!include code/projects/FileCountProgram/FileCountProgram/Program.cs
139139
```
140140
</details>
141+
142+
#. Write a program that
143+
144+
#. Asks the user to enter a path,
145+
#. Checks if there is a file at this path:
146+
#. If there is a file, display its content at the screen.
147+
#. If there is no file, ask the user to enter text. When the user enters exactly "!DONE!", on a single line, without the quotes, write the text entered before !DONE! into a file located at the given path.
148+
149+
Your program should be able to handle graciously possible issues (such as an invalid path).
150+
151+
Below are two examples of execution, taking place one after the other.
152+
153+
154+
### Example execution #1
155+
156+
```text
157+
Enter a path
158+
/͟h͟o͟m͟e͟/͟u͟s͟e͟r͟/͟C͟S͟C͟I͟_͟1͟3͟0͟2͟/͟f͟i͟n͟a͟l͟/͟t͟e͟s͟t͟.͟t͟x͟t͟
159+
Now creating a file at /home/user/CSCI_1302/final/test.txt.
160+
Enter your text, one line at a time. When done, type "!DONE!" (without the quotes), then enter.
161+
T͟h͟i͟s͟ ͟i͟s͟ ͟a͟ ͟t͟e͟s͟t͟
162+
s͟p͟a͟n͟n͟i͟n͟g͟ ͟o͟v͟e͟r͟
163+
t͟h͟r͟e͟e͟ ͟l͟i͟n͟e͟s͟.͟
164+
!͟D͟O͟N͟E͟!͟
165+
File correctly written.
166+
```
167+
168+
### Example execution #2
169+
170+
```text
171+
Enter a path
172+
/͟h͟o͟m͟e͟/͟u͟s͟e͟r͟/͟C͟S͟C͟I͟_͟1͟3͟0͟2͟/͟f͟i͟n͟a͟l͟/͟t͟e͟s͟t͟.͟t͟x͟t͟
173+
Now displaying file at /home/user/CSCI_1302/final/test.txt.
174+
This is a test
175+
spanning over
176+
three lines.
177+
Done displaying file.
178+
```
179+
180+
<details><summary>Solution</summary>```{download="./code/projects/ReadOrWritePath.zip"}
181+
!include code/projects/ReadOrWritePath/ReadOrWritePath/Program.cs
182+
```
183+
</details>

source/solutions/oop/inheritance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tags:
1717
- [ ] `inherits`
1818

1919

20-
## Problem
20+
## Problems
2121

2222
#. Consider the diagram representing the "Room", "ClassRoom", "Office" classes and their relations.
2323

0 commit comments

Comments
 (0)