FindOffset function problem

New to FreeBASIC? Post your questions here.
Post Reply
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

FindOffset function problem

Post by gerry »

Swears at these lines, what's wrong?

Code: Select all

1.bas(10) error 28: Expected pointer, before ',' in 'If Not EOF(FF) Then Get FF, , rByte 'reading'
1.bas(11) error 28: Expected pointer, before ',' in 'If Not EOF(FF) Then Get FF, , Junk 'not used bytes'

Code: Select all

Function FindOffset(ByVal wFile As String, ByVal wMarker As String) As Long
    
    Dim As Long FF 
    Dim As Byte rByte
    Dim As Byte Junk 
    Dim As String CmpStr
    FF = FreeFile
    Open wFile For Binary As FF 
        Do Until EOF(FF)
            If Not EOF(FF) Then Get FF, , rByte 
            If Not EOF(FF) Then Get FF, , Junk 
            If Len(CmpStr) < Len(wMarker) Then
                CmpStr = CmpStr + Chr(rByte) 
            Else
                CmpStr = Mid(CmpStr, 2) + Chr(rByte) 
            End If
                        
            If CmpStr = wMarker Then 
                FindOffset = Loc(FF) + 1 
                Exit Do
            End If
        Loop
    Close #1
    Exit Function
End Function
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: FindOffset function problem

Post by paul doe »

gerry wrote: Feb 20, 2022 17:47 Swears at these lines, what's wrong?
...
You forgot some magicks, like adding the '#' in front of the file handle, like this:

Code: Select all

Function FindOffset(ByVal wFile As String, ByVal wMarker As String) As Long
    
    Dim As Long FF 
    Dim As Byte rByte
    Dim As Byte Junk 
    Dim As String CmpStr
    FF = FreeFile
    Open wFile For Binary As FF 
        Do Until EOF(FF)
            If Not EOF(FF) Then Get #FF, , rByte '' <-- Here
            If Not EOF(FF) Then Get #FF, , Junk  '' <-- and here
            If Len(CmpStr) < Len(wMarker) Then
                CmpStr = CmpStr + Chr(rByte) 
            Else
                CmpStr = Mid(CmpStr, 2) + Chr(rByte) 
            End If
                        
            If CmpStr = wMarker Then 
                FindOffset = Loc(FF) + 1 
                Exit Do
            End If
        Loop
    Close #1
    Exit Function
End Function
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: FindOffset function problem

Post by Munair »

Use #FF (it's a file handle after all).

EDIT, paul doe just beat me there.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: FindOffset function problem

Post by gerry »

paul doe wrote: Feb 20, 2022 18:01
gerry wrote: Feb 20, 2022 17:47 Swears at these lines, what's wrong?
...
You forgot some magicks, like adding the '#' in front of the file handle, like this:

Code: Select all

Function FindOffset(ByVal wFile As String, ByVal wMarker As String) As Long
    
    Dim As Long FF 
    Dim As Byte rByte
    Dim As Byte Junk 
    Dim As String CmpStr
    FF = FreeFile
    Open wFile For Binary As FF 
        Do Until EOF(FF)
            If Not EOF(FF) Then Get #FF, , rByte '' <-- Here
            If Not EOF(FF) Then Get #FF, , Junk  '' <-- and here
            If Len(CmpStr) < Len(wMarker) Then
                CmpStr = CmpStr + Chr(rByte) 
            Else
                CmpStr = Mid(CmpStr, 2) + Chr(rByte) 
            End If
                        
            If CmpStr = wMarker Then 
                FindOffset = Loc(FF) + 1 
                Exit Do
            End If
        Loop
    Close #1
    Exit Function
End Function
oh really, thanks...
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: FindOffset function problem

Post by Munair »

Unfortunately, FreeBASIC is a bit inconsistent here. The # to mark a file handle is sometimes optional as with the Open and Close statement, but it is not with the Get and Put statement, or the Line Input statement. Worse, EOF does not accept the #.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: FindOffset function problem

Post by paul doe »

gerry wrote: Feb 20, 2022 18:04 oh really, thanks...
No probl. A heads up: close #1 should be close #FF. Otherwise you'll have some really hard to track bugs if you intend to use this function elsewhere where files are also handled.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: FindOffset function problem

Post by gerry »

paul doe wrote: Feb 20, 2022 18:01
gerry wrote: Feb 20, 2022 17:47 Swears at these lines, what's wrong?
...
You forgot some magicks, like adding the '#' in front of the file handle, like this:

Code: Select all

Function FindOffset(ByVal wFile As String, ByVal wMarker As String) As Long
    
    Dim As Long FF 
    Dim As Byte rByte
    Dim As Byte Junk 
    Dim As String CmpStr
    FF = FreeFile
    Open wFile For Binary As FF 
        Do Until EOF(FF)
            If Not EOF(FF) Then Get #FF, , rByte '' <-- Here
            If Not EOF(FF) Then Get #FF, , Junk  '' <-- and here
            If Len(CmpStr) < Len(wMarker) Then
                CmpStr = CmpStr + Chr(rByte) 
            Else
                CmpStr = Mid(CmpStr, 2) + Chr(rByte) 
            End If
                        
            If CmpStr = wMarker Then 
                FindOffset = Loc(FF) + 1 
                Exit Do
            End If
        Loop
    Close #1
    Exit Function
End Function
I use it with such a function, but it spoils files, how can I fix it?

Code: Select all

Function WriteToFile(ByVal wFile As String, ByVal wValue As String, ByVal wMarker As String) As Boolean
    
    Dim As Long S
    Dim As Byte i
    Dim As Byte bVal
    S = FindOffset(wFile, wMarker)
    wValue = wValue + "<END>"
    
    Open wFile For Binary As #1 
        For i = 1 To Len(wValue)
            bVal = Asc(Mid(wValue, i, 1)) 
            Put #1, S + (i - 1) * 2, bVal 
        Next i
    Close #1
    
    Return True 
    Exit Function

    Return False 
End Function
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: FindOffset function problem

Post by Munair »

There's no clue without knowing the content of wValue, wMarker.or the file.

Tip:

You can write the byte directly, so instead of:

Code: Select all

bVal = Asc(Mid(wValue, i, 1)) 
Put #1, S + (i - 1) * 2, bVal
you can do:

Code: Select all

put #1, S + (i - 1) * 2, wValue[i - 1]
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: FindOffset function problem

Post by gerry »

Munair wrote: Feb 20, 2022 18:36 There's no clue without knowing the content of wValue, wMarker.or the file.

Tip:

You can write the byte directly, so instead of:

Code: Select all

bVal = Asc(Mid(wValue, i, 1)) 
Put #1, S + (i - 1) * 2, bVal
you can do:

Code: Select all

put #1, S + (i - 1) * 2, wValue[i - 1]
Thanks! :)
Post Reply