As I write the boilerplate, let me explain the background to this programme.
00:06
We're going to learn how to store data more permanently to secondary storage, such as a drive.
00:12
We'll need to use the I O library to do that.
00:15
When we use variables, constants, arrays and lists, the data is held temporarily in the main memory.
00:21
That's the cache and the ram.
00:23
If we close our programme, we lose the data and have to input it again.
00:28
It's more useful to store data in text files so you can load it again in the future.
00:34
There are lots of different standards for structured text files, such as XML and JSON, but to keep things simple, we'll store our data in what's known as a serial text file.
00:45
More complex data can also be stored and retrieved from databases, but that's something we'll learn in the future.
00:52
In the main programme, we want to get the name of a user that we have stored in a file.
00:57
So we'll identify a new variable called user, and that becomes the result of a subprogram we'll write in a moment called load.
01:05
That will require the file name.
01:07
So as an argument, datafile.txt.
01:12
so, just to be clear here, load is not a command, it's a subprogram we need to write.
01:18
Oops, I've written the same line of code twice.
01:20
This is a mistake.
01:22
I'll leave it in to show you that it won't make any difference.
01:24
And these sorts of errors could go unnoticed, lurking in the background, doubling the drive read and write cycles, reducing the lifespan of the drive.
01:33
Maybe the compiler will pick this up as redundant code and remove it.
01:37
Who knows?
01:37
We'd have to analyse the execution of the programme to know.
01:42
Let's just finish the main programme by outputting some text this programme was written by, and then the user variable, which we'll get from the data file.
01:53
Okay, so now let's write the subprogram that will read the data from our datafile.txt file and return it back to the main programme.
02:02
Static string load and we want a parameter, filename.
02:08
We need a variable representing the open file in C.
02:12
We do this with what is known as a stream reader.
02:15
So stream reader file becomes a new stream reader using the filename as an argument.
02:23
The variable file is also known as a pipe.
02:26
It's effectively a communication link between the programme and somewhere on our drive where the file is stored.
02:33
How that all works we'll leave to the operating system.
02:37
The string user, variable then becomes what we read from the file.
02:42
So file readline Note how we can use readline to read from both the keyboard and and the file.
02:50
The data in our, user variable will contain a hidden character at the end that we can't see when we look at the file.
02:57
But it would tell the computer it was the end of a line of data in the file.
03:01
This is known as an end of line marker.
03:04
There may also be an end of file marker too.
03:07
We need to get rid of these and Trim does this for us.
03:13
Important to close and open file as quickly as you can.
03:17
Although modern operating systems are good at preventing file corruptions, it is possible that if our programme or computer crashes with an open file, things can go wrong in the data in the file.
03:28
So it's considered good practise to do the minimum number of commands while the file is open.
03:34
That's why we don't leave closing the file to the end of the subprogram.
03:40
Finally, we want to return the user variable back to the main programme for this programme.
03:46
That's it.
03:46
We could give it a run now and it would do what we want.
03:49
Read the text from the datafire.txt file and output it to the screen.
03:54
However, while we're here, I'll show you how to save data from a programme too.
03:59
As always, for problem decomposition and reusable components, we'll use a subprogram for this static void save.
04:08
And we'll take our user variable, although this could be any variable and the file name as parameters.
04:15
This time we need a stream writer to write to a file.
04:18
So Streamwriter, file becomes a new stream writer using, the file name.
04:25
Note that data can only go in one direction at a time, either from the drive or to the drive.
04:32
Although you can have as many pipes as you want open, so you could have one pipe for reading while another one is writing.
04:41
We now write the user variable to the file using the writeline command we're already familiar with.
04:48
You just can't read and write to the same file at the same time?
04:53
Well, not using this, approach anyway.
04:56
Finally, we close the file.
04:58
Now we have two handy, subprograms for reading and writing a variable to a file.
05:04
If the file exists but is empty, you'll get an empty string from it.
05:08
If the file does not exist, the programme will crash at the moment as it can't find the file.
05:13
We can fix that in future programmes.
05:16
Let's give it a quick run and cheque that the load subprogram works.
05:20
It won't do anything with the save subprogram because we didn't call that?