Planning Example 1 - Text Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
morning everybody this is the unit for lesson one still we're going to have a look at the second topic and that's all planning how to plan a program that reads and writes to text files and we're going to start with a very very basic program program 91. so if we can have a look at program 91 it says code and sdi single document interface application so it's one screen um i must have that's supposed to say names not games add name screw box so there must be a text box to enter a name a label to display a confirmation message and a save name button and then the view names group box with just a list box to display the data and a button to read all names from the file so the save names button writes the name one at a time to a file and read all names reads again the names from the file displays it in the list box the application we've got some shared data so store the name of the file names.txt as a constant save name button what must do get the name from the user save the name to a file we'll need to append it otherwise we delete all the previous data and then we need to save one name per line and display confirmation message to the user and normally we read the screen for the next input if somebody click clicks on the button read all names from file then we simply need to display all the names that's saved in the file in the list box one name per line and we need to have a look at some test data as well so i first of all want to have a look we need to imagine some kind of a screen you'd normally go draw it on paper this is what i want want more release somebody can enter a name we're going to save the name to a file and then click on a separate button to display all the names that's saved in the file okay so always i try to explain how the screen must look as thoroughly as possible go draw a simple diagram for you so that you know exactly how it's got to happen okay so if i go have a look at the planning then program 91 save the names read names from a file doesn't use a clause at all only an application application has got a module one constant so it's capital letters file name type a string the axis would be public and the initial value would be names.txt it's a string so it's got to have inverted quotation marks around it i then have button save name inform names it's a button click event procedure i will need to get a name from a text box so i'll need a name input variable i'll need to display user messages so he's got to use it to be a user interface control i'm going to use the file name from the module and i've got an output file and i want you to tell me what's the type of the fall it's of the type stream writer so my steps would be declare the variables i'm going to get the name from the text box i'm going to open the file for append i'm going to save the name that the user typed in in the file i always close the file after i use it then i read the form for the next input and a display confirmation message to the user okay so the vb steps that we're interested in things that we need to pay attention to is where i open the file it's io dot folder to pentix and i need to specify the file name using a rightline statement we're going to save the name to the file and we need to close the file it's normally a method call like that button read where i read from the file so it's time form still it's a button click event procedure button read names we need a list box we actually doesn't need label message only a list box local variables i will need an input file of the type stream reader i will need to use the name of the file the name of the person every time that i read it from a file and again i use the file name so the steps from that algorithm that i gave you we would need to declare the variables this that the file exists open the file for input if it's existing then a loop to read the data until i reach end of file eof written like that or capital letters is a common acronym for programmers so eof means in the file i will read a line that will give me the name i will save the name in the list box end of the loop after the loop close the file if the fault doesn't exist i need to go and display an error message tell the user there is no fault to read from the statements that i'm interested in would be test if the file is there i will need to clear the list box open the file for reading do one or do until loop to read from the file you guys are more used to the do while so i'm going to use that i read one line save the name that's returned there's only one name per line i displayed in the list box after the loop close the file just a short message over here to tell me what's wrong i can elaborate when i go write the programming code and end if then if i have a look at the test data so i can specify that i'm going to add these three names if i go view the names then it's got to be those three names that's displayed if i add then two more names and view again then i need to see these five names if i add another name and view again then i need to see those six names okay so this would be my test data just the operations that i need in my program every time what would be the things that we are going to have a look at so we need to verify that i can do separate add operations and that each time that the names are added correctly to the file now we can go have a look at coding the program from the planning i've already designed the screen for us so i've got the button to save a name and a button to read the names from the file just so that this video can be nice and quick i've already entered quite a few things so let's just go switch on the options because i forgot options must always be on these my constant it's a public constant it's called file name and that's the names of the false vb and the windows operating system is not case sensitive but doesn't matter really if i use it lowercase in uppercase knee in because i've got a constant that same fall would always be used and then lowercase and uppercase doesn't matter to vb and to the windows operating system if i can have a look at the save data to a file statement then first of all all these english tips you can actually go and you start with that copy from your second year onwards we don't pay attention to planning to to the extent that we're doing with beginners so you're very welcome to then and that's why the statements are over here at the start of the method before you write the code write down the statements for you don't need a separate planning document once you are really a little bit more advanced so we don't need to fill this in but before you write this statement go write down your steps first as a comment before the method then you know exactly what you need to do you've planned it planning doesn't take a lot of time and you're good you're good to go so declare the variables i've got name and output file out file i get the name first from the text box i open the full file to append i write the name i close the file i read the screen for the next input and i tell the user yes the name is saved so if we go have a look at so this is the debug bin debug folder where the names would be saved and currently there's no name there so if i were to run the application and if i use my t-stator just put it like that so first of all sue and my message sue is saved then bin bin is saved joe joe is saved if i close the file now and open it again then have a look these names.txt if i click on it it shows me the data and i can even go and double click and open it in notepad but i can see the data that's saved over there if i then have a look at the code to write order or read the names from the file okay here we've got this steps nice and big so i copied first my steps and now if i have a look at it declare the variables i need the name and an input file as an io.stream reader so io is just the namespace it works like our surnames it tells the vb program we to get the stream reader so stream reader is saved in the io namespace io short for input at output so i first test if the name file exists if it does i clear the text the list box i open the file for input i've got my loop to test for end of file i read the name i save that name in the list box after the loop i close the file if there is a problem i display a nice error message to the user and again if i now run this program i can immediately click on the read the names and i've got sue bin and joe so if i have a look at the rest of the data my steps now says see if i add nico and emma and then again read the data if i didn't clear the list box it would be displayed at the bottom hey if i add another name now lady and again i can read and see that the names exist okay so what's the things that can go wrong i want to have a look at that because it's common mistakes of students that causes errors the first thing would be in the safe part at this stage we need to add the name every time if i use the statement if i use the statement to open the file in create mode what's going to happen in this program so as soon as i write another name and now let's say save it to the file and i click on read then i only get anna if i go save another name click on read i only get benjamin if i can have a look at the file itself only one thing is saved benjamin what is the problem i'm opening the file for in create mode that means any previous data every time is deleted so careful the other thing that would go wrong is if you don't check that the file exists or then if you don't use a constant so if i just use my own name and here i've got names.txt and where i write from the file i for example have name.txt then i'm trying to use two different files so that's why a constant like file name would be a but a lot better to use go to find a constant use it always okay that's it you guys can write program 9293 have a look at them thank you
Info
Channel: BeginnerProgramming
Views: 263
Rating: undefined out of 5
Keywords:
Id: gxgXuSlbbiY
Channel Id: undefined
Length: 16min 57sec (1017 seconds)
Published: Wed Nov 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.