b=BinaryStream.Open(f,true) doesn't overwrite

I have a simple routine as follows:
dim f as folderItem
dim b as BinaryStream
dim n1,n2 as integer
f=GetFolderItem(FilePath)
b=BinaryStream.Open(f,true)
for n1=0 to List.ListCount-1
for n2=0 to List.ColumnCount-1
b.writeshort len(List.cell(n1,n2))
b.write List.cell(n1,n2)
next
next
b.close

This saves the content of a list box. The file has been written previously, and now re- opened
If I delete rows from the List box using List.RemoveRow(List.LastIndex) and then save with the above, when I open the file again, I find that it is still the original length. The deleted rows are reinstated.

I have checked that n1 in the above routine does in fact count only to the new Listcount so it appears that the file is not fully overwritten.

If after deleteing rows, I add another and then save, it saves correctly. So the file is not overwritten only when the data is identical up to the new limit.

Is this expected? I am accustomed to writing files but have never seen this occur before.

FYI if you use the code tag instead of the bold tag you can format your post nicely
Like this

dim f as folderItem
dim b as BinaryStream
dim n1,n2 as integer

f=GetFolderItem(FilePath)
b=BinaryStream.Open(f,true)
for n1=0 to List.ListCount-1
  for n2=0 to List.ColumnCount-1
    b.writeshort len(List.cell(n1,n2))
    b.write List.cell(n1,n2)
  next
next
b.close

Note that the “true” parameter to binary stream.open is not “erase file”
It simply allows you to read & write the file
So you have not overwritten the entire contents just replaced some of it
If you want to completely replace the file delete it then write a new one

Thank you. Can you answer my question?

Sorry, I note an answer at the bottom.
It should not be necessary to revert to a ‘Save as’ routine, simple save should do it.
As far as I can see, the file should be overwritten.

What is the position in the file when you start doing your writes? Perhaps you need

b.Position = 0

to start at the beginning to overwrite. But if the new data is smaller than the old data, the extra data will remain in the file. It seems like deleting it first would be safer.

[quote=65307:@John Sunderland]Sorry, I note an answer at the bottom.
It should not be necessary to revert to a ‘Save as’ routine, simple save should do it.
As far as I can see, the file should be overwritten.[/quote]
This isn’t “save” or “save as”
Binary streams can replace data in an existing file
Thats precisely what it does
see http://documentation.xojo.com/index.php/BinaryStream.Open
You’re expecting it to essentially delete the file & create a new file
Suppose your file had 26 characters in it (the lower case alphabet)
abcdefghijklmnopqrstuvwxyz
Then you open it for read & write & simply write “ABC” back to it
What you’re saying is you want it to only have “ABC”
However not everyone wants that
IF this deleted the file & removed all the rest of the contents (i.e. truncated the file) a LOT of code would be impossible to write
So thats not what it does
IF you did write ABC your file would now contain
ABCdefghijklmnopqrstuvwxyz
Which is exactly the thing you’re seeing
If you expect writing to a file to delete & remove all contents first then you have to write the code to do that
It’s NOT what everyone expects and the code to do it is maybe 1 or 2 lines longer
If thats the effect you want write your code so it entirely replaces the file - not just contents

dim f as folderItem
dim b as BinaryStream
dim n1,n2 as integer

f=GetFolderItem(FilePath)
if f <> nil and f.exists then // if the file already exists we want to delete it to create a new one with JUST the new contents
   f.delete
end if
b=BinaryStream.Create(f,true)
for n1=0 to List.ListCount-1
  for n2=0 to List.ColumnCount-1
    b.writeshort len(List.cell(n1,n2))
    b.write List.cell(n1,n2)
  next
next
b.close

Setting

b.Length=0

before you start writing should wipe the previously existing file contents. See
http://documentation.xojo.com/index.php/BinaryStream.Length

Thank you folks. I see the point. I am just amazed that i have never come cross this before, since I have been manipulating files with RB since 2005.