You are not logged in.

Giasone70

Beginner

  • "Giasone70" started this thread

Posts: 8

Date of registration: May 7th 2011

  • Send private message

1

Monday, May 30th 2011, 11:30pm

Random file

I know the file handling subject was alreaty treated, but i was'n able to find something explaing how to convert this passage:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Private Sub Form_Load()

Dim Reply As Integer

' Generate the full data file path and name.
' We store it in the application directory.
FullFileName = App.Path & "" & FILENAME
FileNum = FreeFile

' Open the data file and calculate the number of records in it..
Open FullFileName For Random As #FileNum Len = Len(CR)
NumberOfRecords = (LOF(FileNum) / Len(CR))

' If the file is empty (just created).
If NumberOfRecords = 0 Then
	Reply = MsgBox("New file - start entering addresses?", vbYesNo, "New file")
	If Reply = vbYes Then
    	EnteringNew = True
    	Form1.Show
    	Call AddNewAddress
	Else
    	Close (FileNum)
    	End
	End If
Else    	' If the file is not empty, display the first record.
	CurrentRecord = 1
	EnteringNew = False
	Call DisplayRecord(CurrentRecord)
End If

End Sub


I'll be glad to everyone will help me to do this.
Alessandro

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Tuesday, May 31st 2011, 2:28pm

Here is a sample how to use random access files:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Option Explicit

'
'  demo for random file access
'

#Import java#io#RandomAccessFile 

Public Sub Command1_Click()
   Dim fileName As String = "myFile.bin"
   Const recLen = 80             '  common record length
   Dim nRec As Integer
   Dim s1 As String = "Good morning! I am String 1 with äöüÄÖÜß"
   Dim s2 As String = "Hi! I am String 2"
   Dim s3 As String = "Hello! I am String 3"
   Dim s As String
   Dim rf As New RandomAccessFile(fileName, "rws")
   
   '  fill our file with three strings
   Call rf.writeUTF(fixedLen(s1, recLen))
   Call rf.writeUTF(fixedLen(s2, recLen))
   Call rf.writeUTF(fixedLen(s3, recLen))
   
   '  loop through file to see if strings are still there
   For nRec = 1 To 3
      Call rf.seek((nRec - 1) * (2 + recLen))
      s = rf.readUTF()   
      Debug.Print nRec & ": " & s
      Debug.Print "FP=" & rf.getFilePointer()
   Next nRec
   
   Call rf.close()
End Sub

Private Function fixedLen(s As String, sLen As Integer) As String
   Dim t As String
   Dim c As String
   Dim i As Integer 
   
   ' clean all chars out of ASCII 30 ..  127 to prevent
   ' variable length string encoding (UTF-8)
   t = ""
   For i = 1 To Len(s)
      c = Mid(s, i, 1)
      If (Asc(c) >= 32) And (Asc(c) <= 127) Then
         t = t & c
      Else
         t = t & "?"
      End If
   Next i
   
   If Len(t) > sLen Then
      fixedLen = Left(t, sLen)
   Else
      fixedLen = t & Space(sLen - Len(t))
   End If
End Function


Random access files are seldomly used in java, because the byte-layout of objects is undefined and not known to the developer.
Jabaco currently does not support passing byte arrays as parameter to function calls.

Hopefully, the sample will inspire you rather than frightening you.

Greetings

A1880

Rate this thread
WoltLab Burning Board