How to truncate filesize in C++

I

iceman

Hi all,
How do I truncate the file size.Is it possible to do this using
fopen ,fseek and fwrite.
Suppose I have a file of 30 KB if I want to reduce the size to
15KB,Howcan I do this without overwriting the the file[ie opening the
file for writing again and writing 15kb]

Itried this but it didnt work.

fseek(iofile,-150,SEEK_END);//150 is a random number
fwrite(numbers, sizeof(char),0,iofile);

Regards,
Jegan
 
P

Phil Endecott

iceman said:
Hi all,
How do I truncate the file size.

I don't think that C++ provides a standard way to do this, but I could
be wrong.

On POSIX systems, you need the ftruncate() or truncate() call. The
former takes a file descriptor and the latter takes a filename. I'm not
sure about other systems; you should ask on a newsgroup specific to your
platform.


Phil.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top