🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Using WriteFile to write to the start of a file

#523
From: Mike Collins
Date: Sat, 26 Jan 2008 18:25
42 lines
3025 bytes
Hi all, feel a little embarrassed to as such a seemingly easy question 
but...

I'm trying to create an event logging system.  I have a main thread that 
monitors certain things and I need to log these in a text based event log. 
Because the file i/o make take some time, I've created a new thread to do 
the actual writing operations.  The main thread pushes events onto the 
secondary threads stl stack.  The secondary thread should then process these 
events in a FIFO order and write them to the event log.  So far i think my 
logic is sound :-).

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.  I 
want my events to be stored in ascending order of when they occurred.  I've 
used WriteFile a million times to create files or write data to the end of 
them i.e. appending them. but i can't find anyway to write to the start of 
the file.  Any suggestions.

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?

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.  I 
would ideally like to perform a check to see if the log has exceeded a 
certain size and then delete the oldest entry.

Finally, are there any issues with performing file I/O from within a thread? 
What happens if the thread's CPU time elapses before the write operation has 
finalised?

Sorry for all the questions, just really can't get my head around this.  I 
had thought of maybe using one of the VCL wrappers i.e. TFileStream or the 
TStrings but i need to ensure that each entry is written as it is processed 
and not simple keep writing the whole stream or strings object each time.

Any help or advice would be gratefully received.

Many thanks,

Mike Collins 
Technical Details
Message-ID: <479b78fd@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!eweka.nl!lightspeed.eweka.nl!195.114.231.69.MISMATCH!feeder.news-service.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,265009ffb540730e,start
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Mike Collins" <its@TheBottomOfThePost>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Using WriteFile to write to the start of a file
Date: Sat, 26 Jan 2008 18:25:47 -0000
Lines: 44
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.3959
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4133
NNTP-Posting-Host: 89.242.138.160
Message-ID: <479b78fd@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1201371389 89.242.138.160 (26 Jan 2008 10:16:29 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!eweka.nl!lightspeed.eweka.nl!195.114.231.69.MISMATCH!feeder.news-service.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail