Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use C/C++ to truncate a file? How?

4,246 views
Skip to first unread message

David Sworder

unread,
Dec 1, 1997, 3:00:00 AM12/1/97
to

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
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

Michael j. Tobler

unread,
Dec 1, 1997, 3:00:00 AM12/1/97
to

in unix, you can use the
truncate( pathname, length) or
ftruncate( fd, length ) function.

in windows environment, it's:

_chsize( f, 2564 ) ;

David Sworder wrote:

--
- Blue Skies -
- michael tobler, mailto:mjto...@ix.netcom.com -

Michael j. Tobler

unread,
Dec 1, 1997, 3:00:00 AM12/1/97
to

in unix, you can use the
truncate( pathname, length) or
ftruncate( fd, length ) function.

in windows environment, it's:

_chsize( f, 2564 ) ;

rhar...@skillnet.com

unread,
Dec 2, 1997, 3:00:00 AM12/2/97
to

>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.

Easy - first call SetFilePointer, then call SetEndOfFile.
This procedure can be used to either truncate or expand a
file.


Pete

unread,
Dec 2, 1997, 3:00:00 AM12/2/97
to

Read what you want from the file to memory, close the file,
delete the file, create and write a new file from memory in the same
directory
with the same name.

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

Pierre Abbat

unread,
Dec 2, 1997, 3:00:00 AM12/2/97
to

Reposition the file pointer and call SetEndOfFile.

phma

Fraser Hutchinson

unread,
Apr 1, 1998, 3:00:00 AM4/1/98
to Pierre Abbat

In article <01bcff3e$c1ac96e0$f050...@phma.trellis.net>,

"Pierre Abbat" <ph...@pop.trellis.net> writes:
>Reposition the file pointer and call SetEndOfFile.

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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


0 new messages