This programme allows the student to enter their name, which is then saved on the end of a file.
00:07
At any time, the programme can be run again, a new student added to the file, and a list of all student names in the file can be output.
00:16
In our boilerplate, we need the I O library for file handling commands and the collections library, because we'll store the student names in a list when they're read from a file.
00:27
In the main programme, we'll call a subprogram called menu that will present the two options for the user.
00:33
Let's write that now.
00:35
Static void main in the menu subprogram, we'll output the two options to the screen.
00:44
That's sign up and show students.
00:47
We'll collect the choice the user makes in a variable called choice that we'll initialise to be an empty string.
00:54
We want to keep asking the user to enter a choice until they either choose number one or number two, so we need a while loop for that.
01:02
While Choice is not a 1 and choice is not 2.
01:07
Ask the user to enter their choice and collect the input.
01:12
Note that we don't use an if command to cheque if an input is valid, we should always use a while loop because we don't know how many invalid inputs the user will make before they enter the number 1 or 2.
01:24
Also note that 1 and 2 are actually strings and not numbers because they're inputs from the keyboard.
01:30
Inputs are always strings, and that's why 1 and 2 are in double quotes.
01:35
Okay, now we can do something based on the valid input switch choice.
01:40
In case the input was one, call the add student subprogram.
01:44
In case the input was two, call the show student subprogram.
01:49
And in each case we need to break out of the structure.
01:52
Once that's done, let's write the subprogram to add a student to the file.
01:58
Static void add student A pipe to a text file we'll identify as.
02:04
File is assigned to be an open file named programming.txt.
02:09
this time we want to add data to the end of the file without deleting what's already stored there.
02:15
So by putting true as an extra argument for the streamwriter, it will append or add to the end.
02:22
Instead of overwriting, Student is assigned the input.
02:27
Write that to a file with file writeline with the data in brackets.
02:32
Student, close the file and output a message to confirm to the user that the data is saved.
02:38
You have been signed up, followed by the student's name.
02:43
It's worth noting here that our file remained open while we waited for the user to input the name of the student.
02:49
This isn't the best idea because if the programme or computer crashes with the file open, it can cause file corruptions.
02:57
Ideally, we should collect the input first and then only open the file when we really need to.
03:03
Now we can write the subprogram to show all the students in the file.
03:08
Static void show students output a helpful message so the user understands what the data is.
03:15
List of strings identified as students is a new list of strings.
03:21
Stream reader file is assigned to be a new stream reader with the file called programming.txt.
03:30
in this programme, instead of reading all the data in one operation, we'll read it a line at a time.
03:36
This would allow you to do something with the data as you read it in.
03:40
For example, in this case we're going to add the students to a list.
03:44
So while the line we read in is not nothing or null, that is something has been read.
03:51
Add to the students list the line of data read from the file, trimming off the hidden end of line characters and then close the file.
04:02
This loop will mean the variable line holds a single line of data from the file in each iteration.
04:08
This is why the end of line markers were helpful when you type at the keyboard and press enter the computer adds that enter character to your file too.
04:17
But you can't see it unless you turn the visibility of formatting codes on in a file editor.
04:25
We can then use a for loop to output each student in the students list.
04:30
We could have output them as we were reading them from the file too.
04:33
Instead of reading them all in and then outputting them all, you might be thinking what's the point of the list?
04:39
Well, there isn't much point in this programme yet, but maybe we want to do something else with the data afterwards.
04:46
For example, sorting the names into alphabetical order.
04:50
Much easier to do if they are a list in memory.
04:53
That's the programme written showing you how to add to an existing file and and read data from a file a line at a time.