[Gambas-user] Error in file read when length is a negative value ?

T Lee Davidson t.lee.davidson at gmail.com
Sun Jan 17 15:06:53 CET 2021


On 1/17/21 8:53 AM, Gianluigi wrote:
> 
> 
> Il giorno dom 17 gen 2021 alle ore 10:03 Vinode Singh Ujlain <ujlain at gmail.com <mailto:ujlain at gmail.com>> ha scritto:
> 
>     EOF .. I was looking at http://gambaswiki.org/wiki/lang/read?nh <http://gambaswiki.org/wiki/lang/read?nh>
> 
>     _Variable_ *= READ* [ *#* _Stream_ *,* ] _Length_ ...
> 
> 
> Hi Vinode,
> I believe you have misunderstood the use of Variable = READ [# Stream,] Length
> 
> If you want to get the last stream you have to write the code like this:
> 
> Public Sub Main()
> 
>    Dim hFile As File
>    Dim sFile As String
> 
>    hFile = Open "./my-demo.txt" For Read
>    Seek #hFile, -4
>    Read #hFile, sFile, 4
>    Print sFile
>    hFile.Close
> 
> End
> 
> Regards
> Gianluigi
> 

The _Variable_ *= READ* [ *#* _Stream_ *,* ] _Length_ ... format works. But, what is needed, as you demonstrated, is SEEK.

The subroutine could look like this:
Public Sub retChars(howMany As Integer) As String

    Dim fh As File
    Dim fStr As String

    fh = Open Subst("&1/demo.txt", Application.path) For Read
    If howMany < 0 Then Seek #fh, howMany
    fStr = Read #fh, howMany
    Close fh
    Return fstr

End



-- 
Lee


More information about the User mailing list