Question Reading a specific line in a text file?

Karl.Green

New member
Joined
Nov 28, 2011
Messages
2
Programming Experience
Beginner
I have been learning VB for about 3 months now at college and decided to enhance my skills by making my own project at home. My project is basically a game where you have to guess if the next number will be higher or lower than the previous version and you can place bets on it. I have done all of this ok but I wanted to create a high score system so that you could save your score.

I can manage to write out to a text file ok so that it looks like this

Highscores

Name
Score

Name
Score

Name
Score

etc. But I am not sure on how to read in a specific line. So for example I want lblHighscore1.text to equal line 3 of the text document (as vb counts the first line as 0)

So far all I have is this;

Dim objReader As IO.StreamReader = New IO.StreamReader("C:\Users\Karl\Desktop\HighScore.txt")

lblHighscore1.Text = (objReader.ReadLine)

I know it does not look like I have got very far but being new I do not know how to procede!
Any help would be greatly appreciated, thanks!
 
To answer the question as asked, you basically can't. To read a specific line in a text file, you must first read each line before it. To read the third line from a file, you need to read the first line and discard it, then read the second line and discard it, then read the third line. It's for this sort of thing that loops exists. You can use a For loop from 1 to 3 and you will read three lines. If you read them into the same variable each time, you'll overwrite the previous value each time and be left with the last one.

I have some advice for your file too. Do you intend for people to open the file to read and edit it? If not then there's no point formatting the file in a human-friendly way. The file is name HighScore.txt so the "Highscore" line at the top is pointless. The blank lines are also pointless. Also, it's easier to put one record on each line, rather than breaking it over multiple lines. If the app is only going to be read and written to by your app then the most appropriate format would be:
VB.NET:
Name,Score
Name,Score
Name,Score
Anything else is superfluous and simply makes automation more difficult. I would also suggest using a TextFieldParser to read the data and a StreamWriter to write it out. The MSDN documentation for the TextFieldParser class has a code example.
 
If the file is not very large (a highscore file is most likely very small), you can read all lines in file and get the line by index using File.ReadAllLines method.

Also, look into using the Xml format when you get the time.
 
Thank you both.
@jmcilhinney I am using a streamWriter to write the file out but I have not heard of a TextFieldParser so I will look into that. Also that is a good point on the way in which I have set the file to be written out. Thank you!

@JohnH Thankyou, I will have a go at your method also and will have a look into the xml format later
 
Back
Top