@coders
Hello;
I'm working with files...
To load an executable , you have to open the file for binary , and then GET one byte at a time..
It takes several minutes to load a *.MP3 of several megabytes.
Is there a way you guys could create a function LOADFILE( filename ) to load a whole binary file , into a string variable??
EXAMPLE: Dim as string File1 = LOADFILE( path \ filename )
Thank You!!
Loading Files
Re: Loading Files
Here is save and load for text or binary.
Code: Select all
#Include "file.bi"
Sub savefile(filename As String,p As String)
Dim As Integer n
n=Freefile
If Open (filename For Binary Access Write As #n)=0 Then
Put #n,,p
Close
Else
Print "Unable to save " + filename
End If
End Sub
Function loadfile(file as string) as String
If FileExists(file)=0 Then Print file;" not found":Sleep:end
var f=freefile
Open file For Binary Access Read As #f
Dim As String text
If Lof(f) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
return text
end Function
dim as string file1=loadfile("path")
print file1
sleep
Re: Loading Files
This runs fast:
@dodicat: again learned something new today -> LOF. Thanx
Code: Select all
#include "file.bi"
Dim As String sMP3 = "YourMP3File.mp3"
Dim As Ulong iFileSize = Filelen(sMP3)
Dim As Ubyte Ptr pMem = Allocate(iFileSize)
Dim As Double fTimer = Timer
Dim As Long hFile = Freefile()
Open sMP3 For Binary Access Read As #hFile
Get #hFile, 0, pMem[0], iFileSize
Close #hFile
? Timer - fTimer
For i As Ubyte = 0 To 10
Print Chr(pMem[i]);
Next
Deallocate(pMem)
Sleep
Re: Loading Files
Thanks You Guys!!
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: Loading Files
Why that complicated?
Code: Select all
Open "MySoundFile.mp3" For Binary Access Read As #1
Dim As String g = Input(Lof(1), #1)
Close 1
Re: Loading Files
Are these equivalent?
Do both work for Windows and Linux? I did some tests, and results look equal. Is one of the two preferred in FB, and if yes, why?
Code: Select all
Dim as string content=Input(Lof(f) , #f)
Dim as ubyte ptr pContent = @content[0]
Code: Select all
Dim as ubyte ptr pContent = Allocate(Lof(f))
Get #f, 1, *pContent, Lof(f)
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: Loading Files
I would prefer the upper one, because FB does the memory management for me, and I can (mis-)use FB's string functions on the content.