[0:00]This is part one of our text file handling series and we're going to be looking at how to read information from a text file into Delphi. Up until this point, whenever you've been using a Delphi program, you've had to ask the user to type in information that you wanted for your program to use. Um the problem with that is like when you close the program that information's gone. So we want to have a way that we can save that information and use it later. Now, in another video, we will look at how do we get information into a text file, but let's pretend we have information already saved in a text file. And when this program opens or when we have a button for example, we want to go fetch information from a text file. Um so we can now store information that can be retrieved at a later stage. We don't have to type in everything when the program gets created for example. So, to understand that, let's just understand what a text file is. A text file is just a little document that contains plain text. There's no formatting involved, it's just text and you can put it in information into that text file in whatever format or layout that you want. Um it's just pure text. So we'll look at a couple of examples. So let's before we get into um the text file and what it must look like, let's look at the the the recipe, the algorithm in how do we get information from a text file and get it into Delphi. So, for this recipe, there's seven steps. So I'm going to go through these seven steps. The first step is we need to create our variables that we need to do this algorithm, this recipe. And so there are two key variables. There could be others that we need, but these are the two key variables I would recommend that you have. You need a text file variable. So if you're not aware of it, there is a variable of type text file and so we go in this, in my example, I'm going to call it my file. So that is a variable of type text file. Okay, and when we get to the recipe, you'll see how we use that variable. The other variable I need is some sort of string variable. So because each line of a text file is some sort of text, plain text. So what I'm going to do when I go through this text file, I'm going to take each and every single line and one line at a time, I'm going to place that line into S Line and then I can do with S Line what I want, whatever the program or whatever the question's asking me to do. So this is step one. Create your variables. Create a text file variable. Create an S line string variable. Then we get to the next six steps. So, here's where we get to the meaty part of the code. So, the recipe or algorithm, the the thing we want to check first, we don't want the program to crash if we try to access a text file that does not exist. So, we need to check if that file exists first. So we're going to use the File Exists function. And there you can see the File Exists in brackets, the parameter is a string and that string must be the name of the text file. Now that text file must be in the same folder as your Delphi files if you're just going to refer to it by its name. So whatever the text file's name is, maybe it's data.txt. Please take note that you must specify the file extension in that string. So, we are checking if the text file exists. If it doesn't exist, so that's why we say if it equals to false, then we will display some sort of message. And then we will exit the triggered event. Now, you don't have to have the exit. You could have an else and then do a begin and then everything for when the file does exist. But what's nice about the exit is that I don't need that else now, because if it gets to that exit, nothing after that exit will then be executed. So we know that that event will stop and the program will not do any other code from that event. So that's the second step. Check if the file exists. If it doesn't, do some suitable messages and exit. Now, what happens if it does exist? Well, the third step is we must assign file. And what we're doing here is you'll notice there we refer to our text file variable. And here's the first case where we're using it. So there, you can see the first parameter is your text file variable and the second parameter is the text file that you want to associate with that text file variable. So you will use, just like we did with File Exists, you will use the name of the text file as long as it's in the same folder and it's got the file extension. So if it was data.txt, you would have that there. And now what's happening is you're associating a link between the text file in that folder with this text file variable. So from this point onwards, we are not going to use the name of the text file or name.txt or whatever the text file's called. From this point on, after we've made this link, we now only use my file because whatever we do to my file will then happen to the text file that we've associated or linked using this assign file. Okay, so that is step three. Now, step four, we need to move what's called a pointer. A pointer is it tells us where in the text file are we working? Are we at the top? Have we moved to the second line? Are we on the fourth line? Where are we in the text file? We want to move that pointer to the very first line. So to do that, we need to reset the text file and you'll notice we're using the text file variable now. So we start the pointer at the first line of the text file. So we know that we are right there at the top of the text file. Now most of the time you will not know how many lines there are in the text file. It's called comprised of multiple lines. Um if you do, if you know there's always going to be ten lines, then you could use some sort of for loop to go through the text file and do each line individually. But majority of the time, you will not know how many lines are in a text file. So in that case, we need to work through each line bit by bit, but we're going to keep doing that until we reach the end of the text file. And so that's where we have our loop. We're going to have a while loop and we're going to say, while we are not at the end of the text file and there in brackets, you see we've got our text file variable. While there are more lines of the text file to keep to be working on, we need to keep on processing. So keep doing this until we while we're not at the end of the text file, keep doing this, keep doing this. When we get to the end of the text file, then we can stop. So that is step five. Now, you will always do multiple things. So you will always have a begin and end after the while because there's a few things that we will do inside here. And then one of the things that we will do, the thing that we normally do straight away is we use what's called a read line procedure there. So there, you can see the read line procedure and that takes in two parameters. It takes in a text file. And then it takes the string that we declared if you remember from step one. So what it's going to do, if you remember, when we said reset, the pointer was at the first line. And if we were at the first line, then obviously we weren't at the end of the file. So, while we weren't at the end of the file, so the while loop is valid. So we're going to do the read line. What read line will do? It will take just the first line and put it into S Line. It will whatever the first line is in my text file, it will put it into S Line and then after that, we can work with S Line and do whatever we want to do. Now, another thing that read line does, not only does it put the value of that first line into S Line, but it automatically moves the pointer to the next line. So when we are finished working with S Line and we go back up to the while loop and checking if we are not at the end of the file, at that particular point, the pointer will be at the next line, at the second line. And so we read that second line and we do with that line what we want to do and then it moves to the next line, to the third line and are we at the are we at the end of the file? No, then work with the third line and so on and so on until we get to the end of the text file. And then the final step. That was step six, the read line. Step seven. When you are finished doing everything you need to do with the text file, you need to close your association with that text file. You need to close all connections with it and that's why we use a close file with the text file variable. This closes the file once the reading is complete. Only do this once you have finished doing everything that you want. That is the recipe. The seven steps to dealing with the text file. We're going to do two examples quickly so we can see how we can actually do this algorithm in Delphi. Okay, so here we've got our data files and you'll notice that in amongst all of my Delphi files, you'll see I've got two text files there. There's a Names and a Teams text file. And let's just have a look. We're going to deal with the Teams one first. So let's just have a look at the Teams one. You can see there's a list of all teams names in the NFL. So there's a whole bunch of team names there are all text. So, now if you want to add a text file, you can just right click in the folder and you can say new, and you can create a text file that way. Another way of doing it is you can go file new and you can go other. And it should give you a whole bunch of other files. And if we go to other files, you'll notice that text file is one of the options and you can go through this method to get a text file. The nice thing about doing this way is that it will add it to your library over here. So you can see here, I've got my text files associated with my library. So I can actually open this text file in Delphi so I can see all the things that were in the text file. So I'm going to have it over here, so we can refer to it while we're doing the code. So let's go through it. We're going to read from the text file and we're going to do all I want to do is take those that list of names, those list of team names and I want to put them into this rich edit control red out display. So, let's go through it. We're going to double click on the button and do the seven steps. So, step one, do you remember step one? That was declare our variables. Okay, let's put that as a comment there. We declare our variables. And there were two variables that we needed. If you remember correctly, the first one was some sort of text file variable. Okay, and the second one was an S Line variable, which will make a type string. Okay, so you remember step one, great. Now, step two. You remember step two, we need to check if the file exists. So, we don't want an error to happen if we try to associate the text file with a file that's not there. So, we're going to say if File Exists, and it needs the actual name of the text file, which in our case, it's Teams.txt and it needs it in some sort of string format. Um so you could actually construct, ask the user to input a name and you could put a .txt at the end, all sorts of wonderful things like that. But there, if the if the file exists, we want to check if the file doesn't exist, so we're checking if that is false. You could also say if not file exists. Both of those are correct. If that file exists, then we want to do two things. I want to show some sort of message to say File not found. And then I'm going to exit this procedure, this button click, so that nothing else after the exit will happen. But that will only execute obviously if the file doesn't exist. If we get to this point here, then we know that the file does exist. So then I will go to step three, which is when we assign the file to our variable. Okay, so remember assign file. This one this procedure takes in two parameters. It takes in our file variable, which is my file. And then it takes the name of the text file, which we know is Teams.txt in some sort of string format. So that was step three. Step four. Then if you've seen the Sound of Music, you'll know we must start at the very beginning. So this let's go, let's put the pointer at the top of the text file. Do you remember what it's what that step is? The reset, reset the text file. Okay, so what that will do, if you look over here, if you can pretend that my cursor over there is the pointer. It will move the pointer over there so that it's ready to read the first line right at the beginning. Not at the end of it, right at the beginning. So that's what reset will do. Okay, we're nearly there. Step five. We now need to loop through our text file. And we're going to do that by using a while loop, while not end of file and we're going to say our text file as the parameter. So while we are not at the end of the file, do the following. So, big big hint here. I'm going to put in a big hint here. You must use begin and end. Please do, because if you don't then you're not you're going to read it and then you're not going to do anything with it. So we've got our begin and our end. That's our end of our while loop. Okay, so that was step five. So we are looping. So we're going to go from now till the end of the text file. Do the same thing every single time. And then we're going to do our step six, which I must make as a comment. We need to get each line of the text file into our string variable. Okay, and for that we're going to use a read line. Readln, and what we're doing is we're taking our text file, which is my file. And we are going to take that line and put it into S Line. So just so that we are clear what's happening at that step is it will, we know that the pointer is now at Arizona Cardinals. It will take the word Arizona Cardinals or the the whole phrase, it will take that whole line and that will now be the value inside S Line. And not only will it do that, but it will move the pointer over there, so that the next time we read line it's at the next line ready to see it. So, now at this particular point here, we now work with S Line. And we can do exactly what we want to do for just one line. I want you to think of this area here as working with just what do you want to do with one line of the text file? And if you can get that right, then you can just believe that this loop will do it for all the lines. So in this case, it's very simple. We just want to take that line, that S Line and display it in our rich edit control, which is register red display lines dot add, and we are just going to put S line into the rich editor control. Now, you could do some editing to S line, which we'll do in another example, but that's all that we're doing for that. We're just taking S line and we're adding it to the rich edit control. And what was our remember, there were seven steps. We've got seven steps. What's the seventh step? Well, once we are finished, so once the loop has finished, this loop will keep going, keep going until it reaches the end of the file, repeatedly adding each line individually. We will come over here to step seven, which I keep forgetting to put my comments in. Step seven, we must close our association. I don't know if that's correct spelling, association with the text file. text file. And that's simply a close file. You'll be surprised how often people forget this last step and you can lose marks if you don't remember that last step. So there is our seven step recipe. Declare our variables, check if the file exists, assign a file to the variable. Start at the very beginning, keep looping until we've reached the end of the file, use each line individually inside our loop, do what we need to do and then closing the file. Let's see if that works. Run, run, run. And there you can see all the names have been added to our rich edit control. It's went and fetched each line individually from the text file and added them one by one until it reached the end of the text file. So there we go, that's how you do text file handling where you read from a text file into Delphi. The key to this everyone is to make sure that you remember your seven step recipe, your seven step algorithm. Remember those step, learn those steps and it'll make your life a lot easier when you are dealing with text files. For more videos in this video series on text file handling, go to our YouTube channel where you can subscribe. Um you can find out whenever we post new videos as well as other videos that could assist you that are currently there. And remember, don't do it the long way, do it the Mr. Long way.

Text Files in Delphi - Reading from a text file
Mr Long Education - IT & CAT
18m 0s3,331 words~17 min read
Auto-Generated
Watch on YouTube
Share
MORE TRANSCRIPTS


