🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1247

Re: Truncate a file from the beginning to the current position

#1247
From: Ted Lyngmo
Date: Wed, 17 Oct 2007 16:10
40 lines
2273 bytes
"Mehmet Caner" <mcaner40@hotmail.com> wrote:
 >
 > is it possible to truncate a file from the beginning ?

Maybe something like this can work for you? It's potentially very 
dangerous, but if you wanna shorten extremely large logfiles with 
content that is not crucial to your business, it may be worth a try.

Best regards,
Ted Lyngmo

void remove_first_bytes( char *filename, size_t bytes ) {
   FILE *fin, *fout;
   char buf[BUFSIZ];
   size_t rco, new_end=-1;

   fin = fopen( filename, "r+b" );
   if( fin ) {
     fout = fopen( filename, "r+b" );
     if( fout ) {
       fseek( fin, bytes, SEEK_SET );

       while( !feof( fin ) ) {
         rco = fread( buf, 1, BUFSIZ, fin );
         if( (rco>0) && (rco != fwrite( buf, 1, rco, fout )) ) {
           // big trouble, file destroyed
         }
       }

       new_end = ftell( fout );

       fclose( fout );
     }
     fclose( fin );

     if( new_end>0 ) {
       // Now use SetEndOfFile( ..., new_end );
     }
   }
}
Technical Details
Message-ID: <471633e2$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!feeder3.cambrium.nl!feeder6.cambrium.nl!feed.tweaknews.nl!xlned.com!feeder1.xlned.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4715d331@newsgroups.borland.com> <4715df73@newsgroups.borland.com> <47161c7b@newsgroups.borland.com> <m89ch3h2tophqsg72tk7on1p092qsb9u3l@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,92368457f5454c39
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Wed, 17 Oct 2007 18:10:26 +0200
From: Ted Lyngmo <tedREMOVETHIS@lyncon.se>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
X-Accept-Language: sv, en, en-us
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Truncate a file from the beginning to the current position
References: <4715d331@newsgroups.borland.com> <4715df73@newsgroups.borland.com> <47161c7b@newsgroups.borland.com> <m89ch3h2tophqsg72tk7on1p092qsb9u3l@4ax.com>
In-Reply-To: <m89ch3h2tophqsg72tk7on1p092qsb9u3l@4ax.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 85.195.27.81
Message-ID: <471633e2$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1192637410 85.195.27.81 (17 Oct 2007 09:10:10 -0700)
Lines: 40
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!feeder3.cambrium.nl!feeder6.cambrium.nl!feed.tweaknews.nl!xlned.com!feeder1.xlned.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail