Skip to content

Commit 7a084cb

Browse files
ClémentClément
authored andcommitted
Finishing adding solution to Quiz #8
1 parent 739dfa5 commit 7a084cb

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.IO;
3+
4+
class Program
5+
{
6+
static void Main(string[] args)
7+
{
8+
// Creating a dummy file with a random number of lines.
9+
string directoryPath = AppDomain
10+
.CurrentDomain
11+
.BaseDirectory;
12+
string filePath = Path.Combine(directoryPath, "dummy.txt");
13+
// Creating a generator for random numbers.
14+
Random gen = new Random();
15+
int linePopulated = gen.Next(20, 100);
16+
// We will store between 20 and 99 numbers in our file.
17+
// We first populate the file with random characters.
18+
try
19+
{
20+
StreamWriter sw = new StreamWriter(filePath);
21+
for (int i = 0; i < linePopulated; i++)
22+
sw.WriteLine((char)gen.Next(32, 126));
23+
sw.Close();
24+
}
25+
catch (Exception ex)
26+
{
27+
Console.WriteLine(ex);
28+
}
29+
30+
// The following count the number of lines
31+
// in the file we created.
32+
int count = 0;
33+
string line;
34+
try
35+
{
36+
StreamReader sr = new StreamReader(filePath);
37+
line = sr.ReadLine();
38+
while (line != null)
39+
{
40+
count++;
41+
line = sr.ReadLine();
42+
}
43+
sr.Close();
44+
}
45+
catch (Exception ex)
46+
{
47+
Console.WriteLine(ex);
48+
}
49+
// We can do a simple check to make sure
50+
// we populated as many lines as we counted.
51+
if(count != linePopulated){
52+
throw new Exception("Something was wrong!");
53+
}
54+
else{
55+
Console.WriteLine($"The file located at {filePath} has {count} lines.");
56+
}
57+
}
58+
}

source/solutions/io/files.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ tags:
5757

5858
#. It does not close the file properly,
5959
#. It can throw exceptions if e.g., `filePath` cannot be accessed by the program,
60-
#. It possibly overwrite the file located at `filePath`.
60+
#. It possibly overwrites the file located at `filePath`.
6161

6262
To solve those, it should be edited to:
6363

@@ -66,6 +66,15 @@ tags:
6666
#. Either test first if the file exists, or use the `StreamWriter` constructor that appends to the file if it already exists.
6767
</details>
6868

69+
#. Assume that `filePath` contains the path to a text file that is not empty. Write a program that counts its number of lines.
70+
71+
<details><summary>Solution</summary>
72+
```{download="./code/projects/FileLineCount.zip"}
73+
!include`snippetStart="// in the file we created.",snippetEnd="// We can do a simple check to make sure"` code/projects/FileLineCount/FileLineCount/Program.cs
74+
```
75+
</details>
76+
77+
6978
#. Write a program that create a text file called `HelloWorld.txt` in its `bin/Debug` folder and store "Hello" followed by a name entered by the user in it.
7079

7180
<details><summary>Solution</summary>

0 commit comments

Comments
 (0)