My question is a simple one: How do I truncate a file?
Let's say that I want to open a file named "MyFile.dat" that has a current
size of 5,000 bytes. I want to move to an offset of 2564 and then truncate
the file.. It looks something like this:
FILE* f=fopen("MyFile.dat","r+b");
if(f)
{
fseek(f,2564,0); //move to the correct offset
now what do I do here to truncate the file??
fclose(f);//ok, i'm done with the file now.
}
Any ideas?
David
in windows environment, it's:
_chsize( f, 2564 ) ;
David Sworder wrote:
--
- Blue Skies -
- michael tobler, mailto:mjto...@ix.netcom.com -
in windows environment, it's:
_chsize( f, 2564 ) ;
Easy - first call SetFilePointer, then call SetEndOfFile.
This procedure can be used to either truncate or expand a
file.
If the file is large the data could be copied a bit at a time
to another (temporary) file, the file would then be subsequently renamed,
after deletion of the original.
David Sworder <dswo...@cts.com> wrote in article
<88102542...@wagasa.cts.com>...
> Hello...
>
> My question is a simple one: How do I truncate a file?
>
> Let's say that I want to open a file named "MyFile.dat" that has a
current
> size of 5,000 bytes. I want to move to an offset of 2564 and then
truncate
phma
from man truncate:
TRUNCATE(2) TRUNCATE(2)
NAME
truncate, ftruncate, truncate64, ftruncate64 - set a file to a specified
length
C SYNOPSIS
#include <unistd.h>
int truncate (const char *path, off_t length);
int ftruncate (int fildes, off_t length);
int truncate64 (const char *path, off64_t length);
int ftruncate64 (int fildes, off64_t length);
Hope this helps,
Fraser
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Fraser Hutchinson Fraser_H...@discreet.com
Software Developer Virtual Set Technology
Discreet Logic Inc, R&D Group Montreal, Quebec Canada
voice:(514) 954-7242 fax: (514)393-1314
Beware of geeks bearing gifs
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=