You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

1

Monday, March 30th 2009, 12:05pm

Reading and writing binary data

Hello guys,

in VB6 you use the two very powerful Commands Put and Get to write arbitrary data in binary files like in the following example:

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
Option Explicit
Dim MyFile As String
Private Sub Form_Load()
   MyFile = Environ("Temp") & "\MyTempfile.txt"
End Sub
Private Sub Command1_Click()
    
    Dim FChnl As Integer
    Dim c As Byte
    
    'if the file exists we delete the file
    If Dir$(MyFile) <> "" Then Kill MyFile
    
    FChnl = FreeFile ' also often used: FChnl = 1
    
    Open MyFile For Binary As FChnl
    
    c = 65
    Do
       Put FChnl, , c
       c = c + 1
    Loop Until c = 65 + 26
    
    Close FChnl
    
End Sub
Private Sub Command2_Click()
    
    Dim FChnl As Integer
    Dim Text() As Byte
        
    FChnl = FreeFile ' also often used: FChnl = 1
    
    Open MyFile For Binary As FChnl
    
    ReDim Text(0 To LOF(FChnl) - 1)
    
    Get FChnl, , Text
    
    Close FChnl
    
    Text1.Text = StrConv(Text, vbUnicode)
    
End Sub


you can do this in Jabaco by learning a bit about streams in the java#io namespace.

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
Option Explicit
Dim MyFile As java#io#File   
Public Sub Form_Load()
   MyFile = New File(System.getenv("Temp") & "\MyTempfile.txt")
End Sub
Public Sub Command1_Click()
   
   'Writing data
   Dim FChnl As java#io#FileOutputStream 
   Dim FPut As java#io#DataOutputStream
   Dim c As Byte
   
   FChnl = New FileOutputStream(MyFile)   
   FPut = New DataOutputStream(FChnl)
   
   'if the file exists we delete the file
   If MyFile.exists Then MyFile.delete
   
   c = 65   
   Do 
      FPut.writeByte(c) 
      c = c + 1
   Loop While c < 65 + 26
   
   FChnl.close
   
   
End Sub

Public Sub Command2_Click()
   
   'Reading data
   Dim FChnl As java#io#FileInputStream 
   Dim FGet As java#io#DataInputStream
   Dim Text() As Byte
   
   FChnl = New FileInputStream(MyFile)
   
   FGet = New DataInputStream(FChnl)
   
   Redim Text(0 To Myfile.length - 1)
   
   FGet.readFully(Text)
   
   Text1.Text = StrConv(Text, vbUnicode)
   
   FChnl.close
   
End Sub




greetings

OlimilO

htillmann

Beginner

  • "htillmann" is male

Posts: 30

Date of registration: Feb 25th 2009

Location: Beverungen / Weser

Occupation: Softwareentwicklung

  • Send private message

2

Friday, May 28th 2010, 10:38am

get in VB

Hi OlimilO,

do you know a way to read a line into a structure, like VB does with get?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Type ini_Struc
   sZeile As String * 2 
   sName As String * 7 
   sValue As String * 3
   CRLF As String * 2
End Type

Dim IniLine As ini_Struc
...
RecLen = Len(InLine)
 Open FileName & ".ini" For Random Shared As #FN Len = RecLen
   Inp_Recs = LOF(FN) \ RecLen
   For i = 1 To Inp_Recs
  	Get FN, , iniLine
  	Text1.Text = Text1.Text & iniLine.sZeile & "Name :" & iniLine.sName & "Wert :" & iniLine.sValue & vbcrlf
   Next i


Ragards
Hubertus

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

3

Friday, May 28th 2010, 11:16pm

Hi Hubertus,



if you occasionally had a short look at the VBTypeClass in the Jabaco-Framework-sources:

http://code.google.com/p/jabacoframework…BTypeClass.java

You maybe noticed that I already began to implement something like the Get-keyword in VB also for Jabaco, last year.

My Idea was to implement something like a binary file access with Get, that could be implemented through Java reflection.

unfortunately I was disturbed because the Framework was not able to compile any more, and moreover I myself ran out of free time.



what you are about to do is impossible due to a completely other reason:

at least concerning the fixed size strings that are possible in VB but not possible in Jabaco until now.

The length descripition of your strings with

As String * X

will be ignored by the Jabaco-Compiler.

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Type ini_Struc
   sZeile As String * 2 
   sName As String * 7 
   sValue As String * 3
   CRLF As String * 2
End Type
Public Sub Command1_Click()
   
   Dim i As ini_Struc
   
   MsgBox Len(i.sZeile)
   i.sZeile = "eins zwei drei"
   i.sName = "blablablablablablablabla"
   i.sValue = "huihuiohuihuihui"
   i.CRLF = "cmoncmoncmoncmon"   
   MsgBox Len(i.sZeile)
   MsgBox i.sZeile
End Sub


maybe it could be possible to reach the goal with an array of char:

Jabaco Source

1
2
3
4
5
6
Type ini_Struc
   sZeile(2-1) As Char
   sName(7-1) As Char
   sValue(3-1) As Char
   CRLF(2-1) As Char
End Type


in VB it is possible to assign a String to a byte (char) array and vice versa, but it's not possible in Jabaco until now, you need extra functions for doing this

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Public Sub Command1_Click()
   Dim i As Integer
   Dim j As ini_Struc
   
   For i = 0 To Ubound(j.sName)
      j.sName(i) = 65 + i
   Next
   MsgBox CharArrayToString(j.sName)
   
End Sub
Function CharArrayToString(arr() As Char) As String
   Dim s As String
   For i = 0 To Ubound(arr)
      s = s & Chr(arr(i))
   Next
   CharArrayToString = s
End Function


beware: sZeile(2) is 3 chars long

you may twist and turn it, its all not very beautiful.



regards

OlimilO

htillmann

Beginner

  • "htillmann" is male

Posts: 30

Date of registration: Feb 25th 2009

Location: Beverungen / Weser

Occupation: Softwareentwicklung

  • Send private message

4

Monday, May 31st 2010, 9:21am

Hi OlimilO,

thanks a lot, i think i am going to do it old fashioned and cut it
into pieces by myself for now.

regards
Hubertus.

Rate this thread
WoltLab Burning Board