PUT# again

New to FreeBASIC? Post your questions here.
Username
Posts: 9
Joined: Jun 22, 2020 5:56

PUT# again

Postby Username » Oct 17, 2020 7:57

I asked this question before but did not get an answer that I could understand. How do I write an exact amount of bytes to a file? When I use put#, it usually generates a file with far more data than specified. Thank you
dodicat
Posts: 6687
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: PUT# again

Postby dodicat » Oct 17, 2020 8:24

I usually do this for files.

Code: Select all


 #Include "file.bi"
function savefile(filename As String,p As String) as string
    Dim As long n=freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:sleep:end
    End If
    return filename
End function

Function loadfile(file as string) as String
   If FileExists(file)=0 Then Print file;" not found":Sleep:end
   dim as long  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

function pre_pend(filename As String,txt As String) as string
    Dim As String s=loadfile(filename)
    If Len(s) Then savefile(filename,txt+s)
    return filename
End function

function ap_pend(filename As String,txt As String) as string
    Dim As String s=loadfile(filename)
    If Len(s) Then savefile(filename,s+txt)
    return filename
End function

savefile("sizetest.dat",string(1,"A"))

print filelen("sizetest.dat")
shell "type sizetest.dat"
print
print
pre_pend("sizetest.dat",string(4,"-"))
print filelen("sizetest.dat")
shell "type sizetest.dat"
print
print
ap_pend("sizetest.dat",string(4,"-"))
print filelen("sizetest.dat")
shell "type sizetest.dat"
print
print
kill "sizetest.dat"
print fileexists("sizetest.dat")
sleep 
fxm
Posts: 9913
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PUT# again

Postby fxm » Oct 17, 2020 8:33

To be more educational than just "look at the documentation" or "look at the example of mine", can you post a short example showing the problem?

(is your file opened in Binary or Random mode?)
Username
Posts: 9
Joined: Jun 22, 2020 5:56

Re: PUT# again

Postby Username » Oct 17, 2020 9:37

fxm wrote:To be more educational than just "look at the documentation" or "look at the example of mine", can you post a short example showing the problem?

(is your file opened in Binary or Random mode?)


dim a as ulongint
a = 1234
open a for binary as #1
put #1,, a

this is an example
fxm
Posts: 9913
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PUT# again

Postby fxm » Oct 17, 2020 9:47

The file size is 8 bytes because you are storing a ULONGINT variable.
(it is the type of the variable which determines the size, and not the variable value)
counting_pine
Site Admin
Posts: 6225
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: PUT# again

Postby counting_pine » Oct 17, 2020 9:51

Your example (with small fixes so it compiles) should, unless I’m mistaken, write 8 bytes of data (the size of a ulongint) to the start of the file. Is that what you’re seeing?

You should be about to write an exact number of bytes in Binary mode with something like: Put #f, , *cast(ubyte ptr, p), n
fxm
Posts: 9913
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PUT# again

Postby fxm » Oct 17, 2020 11:02

For a 2-Bytes size variable, one can also simply use the [U]SHORT type.
Username
Posts: 9
Joined: Jun 22, 2020 5:56

Re: PUT# again

Postby Username » Oct 17, 2020 19:15

fxm wrote:The file size is 8 bytes because you are storing a ULONGINT variable.
(it is the type of the variable which determines the size, and not the variable value)

thank you
im dumb

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests