Files and Records
Files and Records
Files and Records
Files and Records
Files and Records
Files and Records Files and Records Files and Records Files and Records Files and Records Files and Records Files and Records Files and Records
Files and Records Files and Records
Files and Records
Go Back  Xtreme Visual Basic Talk > > > Files and Records


Reply
 
Thread Tools Display Modes
  #1  
Old 12-13-2013, 02:54 PM
SCS17 SCS17 is offline
Newcomer
 
Join Date: Nov 2013
Posts: 3
Default Files and Records


How do I change this code so that the information from the file is shown in just one list box? This is coded in vb6 and does have to contain records.

Private Type highscore
userName As String * 3
userScore As String * 2
End Type

Private Sub printscore_Click()

Dim data(9) As highscore
Dim name_and_score() As String
Dim TotalFile As String

TotalFile = Space(FileLen("C:/high scores.txt"))
Open "C:/high scores.txt" For Binary As #1
Get #1, , TotalFile
Close #1
TotalFile = Replace(TotalFile, vbCr, "")
name_and_score() = Split(TotalFile, vbLf)
For i = 0 To 9
data(i).userName = Left(name_and_score(i), 3)
data(i).userScore = Mid(name_and_score(i), 5)
List1.AddItem data(i).userName
List2.AddItem data(i).userScore
Next i

End Sub
Reply With Quote
  #2  
Old 12-13-2013, 05:17 PM
Gruff's Avatar
GruffFiles and Records Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

You do not seem to be grasping the concept of a random access file.

A random access file is not a sequential text file. Text files can only be read in one direction. Top to bottom.

Random access can read and write records from any position within the file. Random Access files typically cannot be edited in note pad as they contain all the different datatypes stored as byte data not normally as text.

The key is the UDT (User Defined Datatype)
Typically the UDT is several fixed length base datatypes rolled into one structure.

When we talk about these structures within a file we call them records.

Code:
Option Explicit Private Type UserType UserName As String * 25 ' 25 bytes UserAge As Integer ' 2 bytes UserHeight As Integer ' 2 bytes ' Total size of this structure is 29 bytes. End Type Dim User As UserType Dim Users(3) As UserType Private Sub CommandButton1_Click() Users(0).UserName = "Bobby McGee" Users(0).UserAge = 22 Users(0).UserHeight = 63 ' Inches Users(1).UserName = "Billy Kidd" Users(1).UserAge = 16 Users(1).UserHeight = 58 ' Inches Users(2).UserName = "Buddy Holly" Users(2).UserAge = 30 Users(2).UserHeight = 74 ' Inches ' Open file to read / write in 29 byte chunks Open "C:\AAA\TestFile.A1" For Random As #1 Len = Len(User) 'Write three records to the file. Put #1, , Users(0) Put #1, , Users(1) Put #1, , Users(2) 'File now contains 87 bytes (3 * 29 bytes.) Close #1 End Sub Private Sub CommandButton2_Click() ' Open file to read / write in 29 byte chunks Open "C:\AAA\TestFile.A1" For Random As #1 Len = Len(User) 'Read middle record from the file. Get #1, 2, User Close #1 MsgBox User.UserName End Sub

If all you want to do is save and reload a list of information then use a sequential text file not the random access shown above.

Personally I would store the scores in the listbox.itemdata() collection instead
of an array variable.
Use itemdata() when inserting a new score to determine the new score position.

Code:
' Write list Dim i as integer Open "C:\BBB\HighScore.txt" for output as #1 For i = 0 to List1.listcount - 1 Write #1, left(List1.list(i),3) , List1.itemdata(i) Next i Close #1

Code:
' Read list Dim User as string Dim Score as integer Dim i as integer Open "C:\BBB\HighScore.txt" for Input as #1 Do while not eof(1) Input #1, User, Score List1.additem User & " " & Score List1.itemdata(List1.NewIndex) = Score Loop Close #1


RE: Listbox question.
Use a fixed character width font such as "Terminal" and pad the users initials with spaces or underscore characters if needed.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 12-14-2013 at 10:19 PM.
Reply With Quote
Reply

Tags
file handling, highscores, records, scores, vb6


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Files and Records
Files and Records
Files and Records Files and Records
Files and Records
Files and Records
Files and Records Files and Records Files and Records Files and Records Files and Records Files and Records Files and Records
Files and Records
Files and Records
 
Files and Records
Files and Records
 
-->