lizard wrote:Open filename For Append As filenumber
@lizard - Works for me. By replacing "binary" with append it successfully adds data on to the end of the file. I'm still able to read binary as hoped.
This solution fixes half my problem. I want to alter existing data in the file but don't want to rewrite the entire file. Specifically because I won't have all the variables loaded into memory. Maybe the mistake is that I'm wanting to treat hard drive space like memory space. I don't see why I'm not able to open a file, go to a certain position, write data _replacing_ what is already there, without the whole rest of the file becoming zeros.
adele wrote:Open "stuff.bin" for binary access READ WRITE as #ff
@adele - I wouldn't have guessed but READ WRITE "puts" the data without erasion whereas WRITE only "puts" data with erasion to the rest of the data. Thank you for helping me see this.
paul doe wrote:Just let FreeBasic do it's stuff ;)
@paul doe - I can't get your solution to act any differently than my own problem. Below is your code showing that it is suffering from the same issue I'm having.
Code: Select all
type myData
as integer x
as integer y
as integer z
end type
dim as myData stuff( 0 to 2 )
with stuff( 0 )
.x = 1
.y = 2
.z = 3
end with
with stuff( 1 )
.x = 4
.y = 5
.z = 6
end with
with stuff( 2 )
.x = 7
.y = 8
.z = 9
end with
dim as integer fileHandle = freeFile()
open "whatever.bin" for binary access write as fileHandle
put #fileHandle, , stuff()
close( fileHandle )
? "Press a key to load data..."
sleep()
dim as myData loadedStuff( 0 to 2 )
fileHandle = freeFile()
open "whatever.bin" for binary access read as fileHandle
get #fileHandle, , loadedStuff()
close( fileHandle )
for i as integer = 0 to 2
? str( i ) & ": " & str( loadedStuff( i ).x ) & ", " & _
str( loadedStuff( i ).y ) & ", " & str( loadedStuff( i ).z )
next
with stuff( 1 )
.x = 34324
.y = 23
.z = 787
end with
fileHandle = freeFile()
open "whatever.bin" for binary access write as fileHandle
put #fileHandle, 1 * sizeOf( myData ) + 1, stuff( 1 )
close( fileHandle )
fileHandle = freeFile()
open "whatever.bin" for binary access read as fileHandle
get #fileHandle, , loadedStuff()
close( fileHandle )
print
print "now read in the data after having written to the the altered stuff( 1 )"
for i as integer = 0 to 2
? str( i ) & ": " & str( loadedStuff( i ).x ) & ", " & _
str( loadedStuff( i ).y ) & ", " & str( loadedStuff( i ).z )
next
sleep()
@ badidea - Whoa, this works... Below is my code updated to take advantage of seek. What a wonderful tool
Code: Select all
type udt_STUFF
as byte X,Y,Z
end type
dim as long ff, size, position
dim as udt_STUFF STUFF( 1 to 2 )
STUFF( 1 ).X = 3
STUFF( 1 ).Y = 17
STUFF( 1 ).Z = 4
STUFF( 2 ).X = 94
STUFF( 2 ).Y = 87
STUFF( 2 ).Z = 61
ff = freefile
size = sizeof( udt_STUFF )
position = 1
open "stuff.bin" for binary as #ff
seek #ff, position
put #ff,, STUFF( 1 )
close #ff
open "stuff.bin" for binary access read as #ff
get #ff, position, STUFF( 1 )
close #ff
PRINT "1ST 3 BYTES WRITTEN USING SEEK AND READ WITH BINARY"
PRINT "STUFF( 1 ).X " & STUFF( 1 ).X
PRINT "STUFF( 1 ).Y " & STUFF( 1 ).Y
PRINT "STUFF( 1 ).Z " & STUFF( 1 ).Z
PRINT
position += size
open "stuff.bin" for binary as #ff
seek #ff, position
put #ff,, STUFF( 2 )
close #ff
open "stuff.bin" for binary access read as #ff
get #ff, position, STUFF( 2 )
close #ff
PRINT "2ND 3 BYTES WRITTEN USING SEEK AND READ USING BINARY"
PRINT "HERE I PROVE SEEK DOES WHAT APPEND IS INTENDED TO DO"
PRINT "STUFF( 2 ).X " & STUFF( 2 ).X
PRINT "STUFF( 2 ).Y " & STUFF( 2 ).Y
PRINT "STUFF( 2 ).Z " & STUFF( 2 ).Z
PRINT
position = 1
open "stuff.bin" for binary access read as #ff
get #ff, position, STUFF( 1 )
close #ff
PRINT "1ST 3 BYTES READ WITH BINARY"
PRINT "NOTICE THAT THEY HAVEN'T BEEN ERASED"
PRINT "STUFF( 1 ).X " & STUFF( 1 ).X
PRINT "STUFF( 1 ).Y " & STUFF( 1 ).Y
PRINT "STUFF( 1 ).Z " & STUFF( 1 ).Z
PRINT
STUFF( 1 ).X = 100
STUFF( 1 ).Y = 101
STUFF( 1 ).Z = 102
position = 1
open "stuff.bin" for binary as #ff
seek #ff, position
put #ff,, STUFF( 1 )
close #ff
open "stuff.bin" for binary access read as #ff
get #ff, position, STUFF( 1 )
close #ff
PRINT "CHANGE STUFF( 1 ) DATA AND WRITE BACK IN USING SEEK"
PRINT "UPDATED 1ST 3 BYTES THEN READ USING BINARY"
PRINT "STUFF( 1 ).X " & STUFF( 1 ).X
PRINT "STUFF( 1 ).Y " & STUFF( 1 ).Y
PRINT "STUFF( 1 ).Z " & STUFF( 1 ).Z
PRINT
position += size
open "stuff.bin" for binary access read as #ff
get #ff, position, STUFF( 2 )
close #ff
PRINT "2ND 3 BYTES READ ONLY USING BINARY"
PRINT "NOTICE THAT THEY HAVEN'T BEEN ERASED EITHER"
PRINT "STUFF( 2 ).X " & STUFF( 2 ).X
PRINT "STUFF( 2 ).Y " & STUFF( 2 ).Y
PRINT "STUFF( 2 ).Z " & STUFF( 2 ).Z
PRINT
PRINT "IT WORKS AS FAR AS I CAN TELL USING SEEK. APPEND WILL ONLY"
PRINT "ADD DATA ON TO THE END OF A FILE. BUT FOR ALTERING EXISTING"
PRINT "DATA IN A FILE I USED SEEK. THIS EXAMPLE PROVES IT WORKS."
PRINT "THIS EXAMPLE SHOWS THAT USING THIS SEEK METHOD INSTEAD OF"
PRINT "APPEND WORKS FOR WHAT APPEND IS INTENDED TO DO."
PRINT
PRINT "ALL THIS AND THE FILE REMAINED 6 BYTES AS HOPED"
sleep
jj2007 wrote:We have treated these issues in the
Set EOF thread just a few weeks ago.
@jj2007 - Sorry I missed this when searching for existing problems/solutions. EOF wasn't and isn't what I was concerned about but maybe I should be. I will be sure to read through this whole other thread you recommend.