Visual Basic Programming lesson 12 – Reading data from a text file

Visual Basic Programming lesson 12 – Reading data from a text file


Hey guys welcome to another visual basic
tutorial by the Magic Monk. This tutorial is actually requested by one of the
YouTube users who needs some help with an assignment so here it is the first thing
i want to talk about is how to open up a text file and load up the text file and
print the contents in your program now the reason why you want to be able to do
that is because later on we’re going to teach you guys how to save data into a
text file but that’s a bit more complicated so the first thing we’re
going to do is learn how to open up the file first okay so the first thing I
want you guys to do is click on new project so I’ve just opened up Visual
Basic 2010 Express and so right now this is just a welcome screen and I want you
guys to click on new projects and I just want a windows form application and the
name of it I’m going to call it text file tutorial 1 text file tutorial 1 so
I’m going to click OK and you can see that the project has been created now what
I want you to do is click save save all and then where it says save project I
want you to remember the location of the project and the name of the project and
click Save ok so now that we have saved the project I’ll make sure you really do
click Save or otherwise it’s not going to work what we’re going
to do now is simply in this project create a button and a label now what’s
going to happen is when I click this button we’re going to load up a
text file and the contents of that text file is going to be displayed in
this label okay so I’m going to call this button read from file so click on
that and change the text to read from file so now you can see the button as
the words have just changed and now we’re going to double click on the
button and we’re going to enter in some code for the button and the first line
of code I’m going to type in dim space file num as integer equals three
file brackets now what this does is the free file function what it does is it
returns an integer our value that represents the next file number
available so basically every file that you open up is going to have a file
number and people who get mixed up with which file number represents which file
they’ll probably lose track of what file numbers they have used so free file is
simply a function that returns a number where there’s a spare integer value for
your file to be assigned for you to your file so anyway that integer is going to be
stored in the variable file num and I’m going to create declare another variable
called string sorry temp s as string and we’re going to
explain what that does in a second but basically right now all you need to
understand is it’s a string variable called temp s and I’m also going to
declare another another string variable called tempL okay so we have three
variables the first string variable has a value of nothing and the
second string variable has not been assigned any values okay now we’re going
to run the function file open and this is the function that opens the text file
now the first argument that it wants is the file number now notice how we have
already stored the file number into a variable called file num so that’s the
first thing we typed in and then the next thing it wants is the file name as a
string now I’m just going to put in “test.txt and
we’re going to create that file very soon actually let’s call it score.txt
and we’re going to create that file in a second and then you’re going to type in
openmode. and you can see there’s five options here and basically you need
to decide what you’re opening your file up for if
you’re opening it to write to it or to read from it or to append to it now
right now we just want to read from it so it’s OpenMode.Input and you can
think of this as inputting the contents of that file into our program so that’s
why it’s called input ok and now we want to use a do until loop now the
difference between do until and do-while is that do until will repeat a
set of instructions until this condition here is true so basically right now this
condition is false so it will keep repeating everything that I’m putting in
this section now what does this part say this part is saying that until the end
of the file has been reached right so this is file number whatever number it
is we’ve opened it up and we’re going to keep working on whatever it is that we
tell it to do until we reach the end of the file so what are we going to do well
that’s going to require us to use the temp l variable which we created earlier
and what we’re going to do is we’re going to get one line at a time so this
is going to get the first line so line input basically it reads a single line
from the file so it’s going to read that first line from this file number which
is school.txt so it’s going to read that that first line and put the
contents into the variable tempL and then what we’re going to do is
we’re going to add this whole line into our string variable tempS so tempS is
going to equal sorry I’m going to use the increment operator so I’m adding
temp string to temp line so basically TempS is going to contain
what it had before plus temp L and then I’m going to add a line break which is
represented by this variable so this represents a return character which is a
line break okay so now we’re going to keep looping this and then I’m going to
close the file and the last thing I want to do is I
want to display everything in TempS which is going to contain the whole file
into the label so Label1.Text is going to equal TempS okay now I want you
to now click run to start debugging and see what happens so save it first ctrl s
click the play button or debug button and then you will see the application
with a button you can press so press that button and you will see an error
what is the error it cannot find your text file so that is all part of the
plan so now we’re going to copy this file
path and I’m going to open up this file this folder
according to that path so that path is located here all right so I’ve just
opened it up from my computer and within this folder I’m going to right-click and
create a txt files a new text document and I’m going to call it score.txt and
what am I going to put in this file all right now I’m just going to put in my name and let’s say a score that I got from Tetris or whatever game
that I have played and then I might do it for my brother as well okay so I’m
going to save this file close it close this folder I’m going to stop it and I’m
going to run this file run this program again and this time when I click read
from file it reads my text file and puts it in the
label okay so hopefully you got something out of this tutorial see you
again next time.

33 thoughts on “Visual Basic Programming lesson 12 – Reading data from a text file”

  1. can you read a string and an integer from the file like a card from 2 to 10 but 11 will become J the following are Q K and Ace. i'm trying to make a card game.

  2. I've been programming Visual Basic 6.0 for years and I've trying to learn Visual Basic .Net on and off for awhile and file manipulation has been the biggest hangup because it is somewhat different than VB6.0.
    These tutorials makes it seem easy to do and I hope to be able learn VB.Net from them.

  3. Excellent tutorial! How would you modify the code in your tutorial to automatically read the variables from the variable file on the start up of the application rather than from the "button.click"?

  4. I have been scouring YouTube for a thorough lesson that covers these details and yours is the first that covered the full steps of it. I really appreciate the video!
    Gonna continue with the series as I type this!
    Thanks!

  5. Can you make an Tutorial like this but with webclient like FileOpen(FILENUM, WEB.DownloadStrings "GOOGLE.de/TEST.TXT", OpenMode.Input)

  6. Hi. What if the text file already exist but is on a different folder path? And the text file needs to be on that folder.

  7. How do one get data from a data file from WOT REPLAY file, I would like to analyse my performance, can you give a hint here.

  8. Кулер Малиночив

    I wrote a program where you can select the extension, the path, the name of the file, and you can write the multi-line text inside the program. Then you just click the button and it makes a new file with the wroten extension, name, and the path and text, or it overwrites the existing file.

  9. I HAVE A QUESTION
    you know in gcode (a type of text file ) i have such as this lines

    x10 y12 z20 f100

    i want to make a simple app which reeds the line and send the x,y and z values to each its own variable this means after input this valuse to textbox and for example click read button the x variable value will change to 10 and y=12 and z like this

    whats should i do

  10. I am looking for online tutor for visual basic 2010 express. Please refer me to one of the visual basic expert.

  11. I am working on a project that requires reading a simple csv text file. I struggled for days trying out code that I found on the web. Couldn't get StreamReader code to read single records. Then I stumbled on this. a very simple method that works like a charm! Yu saved my bacon. Thanks a million!

Leave a Reply

Your email address will not be published. Required fields are marked *