🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages Started by Mehmet Caner Wed, 17 Oct 2007 09:16
Truncate a file from the beginning to the current position
#1241
Author: Mehmet Caner
Date: Wed, 17 Oct 2007 09:16
12 lines
1473 bytes
Using the code (Remy's code in the archive) below, I can truncate a file 
from the beginning to the current position. Can I do the same operation 
using Windows API?

TFileStream *OldFile = new TFileStream("c:\\oldfile.ext", fmOpenReadWrite | 
fmShareDenyWrite);
OldFile->Position = 100;

TFileStream *NewFile = new TFileStream("c:\\newfile.ext", fmCreate);
NewFile->CopyFrom(OldFile, OldFile->Size - OldFile->Position);

OldFile->Size = 100; // uses SetEndOfFile() internally
Re: Truncate a file from the beginning to the current position
#1242
Author: PaoloItaly
Date: Wed, 17 Oct 2007 10:11
1 lines
920 bytes
SetFilePointer + SetEndOfFile 
Re: Truncate a file from the beginning to the current position
#1244
Author: Mehmet Caner
Date: Wed, 17 Oct 2007 14:29
4 lines
1153 bytes
> SetFilePointer + SetEndOfFile

Thanks for your quick answer. But is it possible to truncate a file from the 
beginning ? 
Re: Truncate a file from the beginning to the current position
#1246
Author: Alan Bellingham
Date: Wed, 17 Oct 2007 15:10
21 lines
1719 bytes
"Mehmet Caner" <mcaner40@hotmail.com> wrote:

>> SetFilePointer + SetEndOfFile
>
>Thanks for your quick answer. But is it possible to truncate a file from the 
>beginning ? 

You mean, can you say "the new content of the file is from here to the
end"?

Sorry, but no.

You'll have to create a new empty file, and copy to it the data you want
kept. The ability to change where a file ends is easy for filing systems
(and fairly vital too, when you're appending data to it in the first
place), but not for where it starts.

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
Re: Truncate a file from the beginning to the current position
#1247
Author: 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 );
     }
   }
}
Re: Truncate a file from the beginning to the current position
#1149
Author: Remy Lebeau (TeamB)
Date: Wed, 17 Oct 2007 16:44
90 lines
3860 bytes
"Mehmet Caner" <mcaner40@hotmail.com> wrote in message 
news:4715d331@newsgroups.borland.com...

> Can I do the same operation using Windows API?

Of course.  The VCL uses the Win32 API internally, afterall.  For example:

    HANDLE hOldFile = CreateFile("c:\\oldfile.ext", GENERIC_READ | 
GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 
NULL);

    if( hFile != INVALID_HANDLE_VALUE )
    {
        ULARGE_INTEGER ulPosition;
        ulPosition.QuadPart = 100;
        ulPosition.LowPart = SetFilePointer(hOldFile, ulPosition.LowPart, 
(PLONG)&ulPosition.HighPart, FILE_BEGIN);

        if( (ulPosition.LowPart != 0xFFFFFFFF) || (GetLastError() == 
NO_ERROR) )
        {
            ULARGE_INTEGER ulSize;
            ulSize.LowPart = GetFileSize(hOldFile, &ulSize.HighPart);

            if( (ulSize.LowPart != 0xFFFFFFFF) || (GetLastError() == 
NO_ERROR) )
            {
                ulSize.QuadPart -= ulPosition.QuadPart;

                HANDLE hNewFile = CreateFile("c:\\newfile.ext", 
GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 
NULL);

                if( hNewFile != INVALID_HANDLE_VALUE )
                {
                    BYTE buf[1024], *ptr;
                    DWORD dwRead, dwWritten;
                    bool bOk = false;

                    while( ulSize.QuadPart != 0 )
                    {
                        if( !ReadFile(hOldFile, buf, sizeof(buf), &dwRead, 
NULL) )
                        {
                            bOk = false;
                            break;
                        }

                        if( dwRead == 0 ) // unexpected EOF
                            break;

                        ulSize.QuadPart -= dwRead;

                        ptr = buf;
                        do
                        {
                            if( !WriteFile(hNewFile, ptr, dwRead, 
&dwWritten, NULL) )
                                break;

                            ptr += dwWritten;
                            dwRead -= dwWritten;
                        }
                        while( dwRead != 0 );

                        bOk = (dwRead == 0);
                    }

                    CloseHandle(hNewFile);

                    if( bOk )
                    {
                        ulPosition.QuadPart = 100;
                        ulPosition.LowPart = SetFilePointer(hOldFile, 
ulPosition.LowPart, (PLONG)&ulPosition.HighPart, FILE_BEGIN);

                        if( (ulPosition.LowPart != 0xFFFFFFFF) || 
(GetLastError() == NO_ERROR) )
                            SetEndOfFile(hOldFile);
                    }
                }
            }
        }

        CloseHandle(hOldFile);
    }


Gambit 
Re: Truncate a file from the beginning to the current position
#1153
Author: Mehmet Caner
Date: Wed, 17 Oct 2007 20:00
1 lines
1100 bytes
Thanks to all for your kind answers. Remy's code is what I need. 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads