Random access files

Domingo

Member
Licensed User
Longtime User
I want to use the random files in basic4android. In basic4ppc it was:

FileOpen (c1, "text.dat",cRandom)
s = FileGet (c1, 5,10) 's will be a string with 10 characters starting from the sixth character in the file.
FileClose(c1)


What is the same sentences in basic4android?

Thanks
 
What exactly are you trying to do?
To open a file you can use code similar to:
Code: Copy
Dim raf As RandomAccessFile
raf.Initialize(File.DirRootExternal, "1.dat", False)
Dim i As Int
i = raf.ReadInt(12) 'reads an integer stored in the 12 position.
Dim buffer(1024) As Byte
raf.ReadBytes(buffer, 0, 100, 1000) 'reads 100 bytes starting at position 1000
Dim s As String
s = BytesToString(buffer, 0, 100, "UTF-8") 'convert the bytes to string
 
Upvote 0
Hi Erel
I tried the example that your post but do the next error:

Error compiling program.
Error description: Cannot cast type: {Type=Byte,Rank=0} to: {Type=Byte,Rank=1}
Occurred on line: 92
buffer = acceso.ReadBytes(buffer,0,100,1000)
Word: buffer


What is the problem?
 
Upvote 0
buffer = acceso.ReadBytes(buffer,0,100,1000)
It's an error by Erel. ReadBytes returns an integer count of bytes read and so cannot be assigned to an array variable. It should be something like
Code: Copy
Dim bytesread as Int
bytesread = acceso.ReadBytes(buffer,0,100,1000)
If I remember correctly ReadBytes doesn't guarantee to always return the numbert of bytes requested - but probably will in the case of a file. It might be wise to check the return value for the correct number of bytes.
 
Upvote 0
Can someone please show me how to get the file size using the .Size property with RAF's? Thank you...

Update: I got it!
Code: Copy
   If File.ExternalWritable = True Then
      'SD Card Installed
      pFilePath = File.DirRootExternal
   Else
      'No SD Card
      pFilePath = File.DirInternal
   End If
   raf.Initialize(pFilePath,pFilename,True)
   mFileSize = raf.Size
 
Last edited:
Upvote 0

Similar Threads

Back
Top