🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#1149
From: 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 
Technical Details
Message-ID: <47163c29$2@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4715d331@newsgroups.borland.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
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4715d331@newsgroups.borland.com>
Subject: Re: Truncate a file from the beginning to the current position
Date: Wed, 17 Oct 2007 09:44:41 -0700
Lines: 92
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 207.178.157.90
Message-ID: <47163c29$2@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1192639529 207.178.157.90 (17 Oct 2007 09:45:29 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
X-Original-Bytes: 3714