Loading Files

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

Loading Files

Postby albert » Mar 05, 2019 18:31

@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: 5771
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Loading Files

Postby dodicat » Mar 05, 2019 18:41

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: 317
Joined: May 05, 2017 19:59
Location: Germany

Re: Loading Files

Postby UEZ » Mar 05, 2019 18:45

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: 4775
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Loading Files

Postby albert » Mar 05, 2019 23:58

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

Re: Loading Files

Postby grindstone » Mar 07, 2019 9:41

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

Re: Loading Files

Postby jj2007 » Mar 07, 2019 16:42

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: 640
Joined: May 05, 2015 5:35
Location: Germany

Re: Loading Files

Postby grindstone » Mar 07, 2019 20:24

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.

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests