Truncate a file

General FreeBASIC programming questions.
Post Reply
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Truncate a file

Post by Jawade »

Is there a way to truncate a file? Like this:

Open "Myfile" For Binary As #1
LOF(1) = &h2000
Close #1
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Truncate a file

Post by grindstone »

It's a little more complicated, but still quite easy:
Dim As String filename = "myfile"
Dim As String tempfile = "temp"

Open filename For Binary As #1
Open tempfile For Output As #2
Print #2, Input(1024, #1);
Close
Kill filename
Name(tempfile, filename)
Alternatively, if the file isn't too big, you can use a string as buffer:

Code: Select all

Dim As String filename = "myfile"
Dim As String buffer

Open filename For Binary As #1
buffer = Input(1024, #1)
Close #1
Open filename For Output As #1
Print #1, buffer;
Close #1
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Truncate a file

Post by fxm »

Yes, or as an alternative, replace the temporary file with a temporary string:

Code: Select all

Dim As String filename = "myfile"
Dim As String s

Open filename For Binary Access Read As #1
s = Input(1024, #1)
Close #1
Open filename For Output As #1
Print #1, s;
s = ""
Close #1
[/s]

[edit]
Post cancelled due to code duplication with the above alternative.
Last edited by fxm on Aug 02, 2018 10:48, edited 2 times in total.
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Re: Truncate a file

Post by Jawade »

Yes, I did it on that way, with a temp file or a temp string.
I hoped there was a way to do it with 1 move.

Thanks.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Truncate a file

Post by St_W »

There's such a function in the CRT, which you can use (and is included in every FB binary anyway, so doesn't cause any bloat or additional dependencies). See ftruncate (or chsize on windows).
See also https://freebasic.net/forum/viewtopic.p ... 27&p=47265
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Re: Truncate a file

Post by Jawade »

Very interesting, I go at work with it.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Truncate a file

Post by dodicat »

To keep truncicated file data,looks like you should use "a" -- append.

Code: Select all



#include "file.bi"
#include "crt.bi"

Sub save(filename As String,text As String)
    Var f=fopen(filename,"w")
    fprintf(f,"%s",text)
    fclose(f)
End Sub

Sub truncicate(filename As String,l As Long)
    Var f=fopen(filename,"a") '<--------- append
    Var handle=_fileno( f )
    _chsize(handle,l)
    fclose(f)
End Sub

Dim As String f="abcdefghijklmnopqrstuvwxyz"
f+=Ucase(f)+Chr(13,10)
f+=f
f+=f
f=rtrim(f,Chr(13,10))'take off chr(13,10)
'===========
save("test",f)
Print Filelen("test")
Shell "type test"
Print
truncicate("test",58)
Print Filelen("test")
Shell "type test"
Print
truncicate("test",5)
Print Filelen("test")
Shell "type test"
print
Sleep  
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Re: Truncate a file

Post by Jawade »

It's very nice. Thanks.
Post Reply