Loading Files

General FreeBASIC programming questions.
Post Reply
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Loading Files

Post by albert »

@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!!
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Loading Files

Post by dodicat »

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


 
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Loading Files

Post by UEZ »

This runs fast:

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
@dodicat: again learned something new today -> LOF. Thanx
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Loading Files

Post by albert »

Thanks You Guys!!
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Loading Files

Post by grindstone »

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
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Loading Files

Post by jj2007 »

Are these equivalent?

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)
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?
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Loading Files

Post by grindstone »

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.
Post Reply