Skip to main content
GameDev.net gamedev.net
🔒 Locked

Truncating a file

Started by Evil Bill Oct 30, 2002 at 11:19 AM 3 replies 200+ views
Original Post
Evil Bill
Evil Bill
Hi, does anyone know of a stream I/O function to set the length of a file? I''ve got a sort of primitive database, and it needs to remove a record in the middle of the file, which means shifting all data after that left, and then shrinking the file. Is this possible, or will i need to write to a new file, delete the old file, and then rename the new one? Cheers, Steve Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
S1CA
S1CA
seek to where you want to trucate, then:

SetEndOfFile()

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Evil Bill
Evil Bill
Cheers, but i don''t suppose theres any way to do that with the e.g. _fsopen(), freasd(), etc. functions?

On a side note, i was stacing into fopen() (as you do ) and i found that it just ends up calling OpenFile(). Is it then more efficient to use OpenFile() than fopen()? I always thought that fopen would be more efficient, but i dunno any more...

Cheers for the reply Simon,
Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
S1CA
S1CA
As far as I''m aware, that''s the only way to truncate a file without copying.

fopen() and the like are ANSI compliant wrappers around lower level functionality. In terms of closeness to the metal it goes something like (reverse order):

C++ streams
fopen()
_open()
CreateFile()/OpenFile()
Kernel calls & hidden NT stuff
Driver interfaces & nastiness


Opening a file is pretty slow on any "secure" OS (2K, XP, NT) due to all the "is this user allowed to do this", encryption etc.

The extra control you get from CreateFile allows you to specify exactly what you''re going to do with the file and who else can play at the same time. fopen() doesn''t have that control so goes for the generic (and sometimes slower) case. That''s why monolithic/PAC/PAK files are a good idea - you keep a big file open and all the open calls go to you instead of the OS.

Fastest way to access files is once again to specify your exact usage, access, sharing and caching types to CreateFile. If the underlying cache mechanism knows you''re always accessing sequentially (FILE_FLAG_SEQUENTIAL_SCAN or summat), it can do a better job of pre-caching.
Memory mapped files are a super fast way of accessing data in a file since they''re actually part of the virtual memory system - much better to just make the file appear as a block of memory rather than allocate some memory, have the system turn it into virtual memory (i.e. file), read data into that memory (page back from file AND read from other file into that data), page it back out when you don''t access it etc!



--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Evil Bill
Evil Bill
K, thanks, i''ll do a bit of jiggling about with my file IO i think.

Cheers again,
Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.