🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Using WriteFile to write to the start of a file

#533
From: Remy Lebeau (TeamB)
Date: Mon, 28 Jan 2008 17:49
49 lines
3128 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message 
news:479b78fd@newsgroups.borland.com...

> However, I cannot find any reference or explanation on how to
> use the WriteFile() api to insert a new piece of data at the start
> of the file.

There is no way to insert data at the beginning of a file while preserving 
the data that was already present.  Writing to a file always overwrites 
whatever was previously present at the position you are writing to.  To do 
what you are attempting, you must create a new file, write the new record 
first, then copy all of the existing records from the old file to the new 
file, then replace the old file with the new file, and then repeat 
everything again for each new record you want to insert.

Otherwise, you would be better off simply writing the records to a single 
file in descending order normally, and then write your own log viewer 
program that can display the records in reverse order.

> Secondly, I'm looking to achieve the best performance possible so with
> this in mind, is it more resource intensive to write new events to the 
> start
> of a file as opposed to appending them to the end?

Yes, a lot more.

> Thirdly, I need to ensure that the event log does not exceed a certain
> size. In either of the above cases (writing at the start or writing at the
> end) how would i approach the problem of removing an entry from
> my event log.

If you write records to the front of the file, then removing old records is 
simply a matter of truncating the existing file to the appropriate size via 
SetFilePointer() and SetEndOfFile().

If you write records to the end of the file, then removing old records 
requires creating a new file, seeking the old file to the starting position 
of the first record you want to keep, copying the remaining records from the 
old file to the new file, then replacing the old file with the new file.

> I would ideally like to perform a check to see if the log has exceeded a 
> certain size and then delete the oldest entry.

You can query the file size before you write any data to it, and you can 
calculate the size of the record you want to write.


Gambit 
Technical Details
Message-ID: <479e156e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <479b78fd@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,265009ffb540730e
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: <479b78fd@newsgroups.borland.com>
Subject: Re: Using WriteFile to write to the start of a file
Date: Mon, 28 Jan 2008 09:49:18 -0800
Lines: 51
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: <479e156e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1201542510 207.178.157.90 (28 Jan 2008 09:48:30 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail