tags:

views:

343

answers:

2

I want to truncate the file something like setsizeof() with FILE * I'm developing vs 2003 windows

#include <unistd.h> there's no such lib

how can I do it freopen() truncates all the data vut doesn't write- getting EINVAL error some help?????

+1  A: 

There are a number (roughly 20) of header files in the C standard and unistd is not one of them (it's a POSIX/UNIX95/UNIX98 header). So there's no requirement for a vendor to provide it. Neither C89 nor C99 have unistd as one the the mandated header files.

The easiest way to truncate a file is to reopen it in write mode (assuming you have the file name).

fclose (fh);
fh = fopen ("file_name", "w");

If all you have is the file handle, you need to use freopen(). You will only get EINVAL if the mode is incorrect. You cannot change the mode except accoording to the following table:

r        -> r
w a      -> a w
r+ w+ a+ -> any mode

See man freopen for further details.

paxdiablo
A: 

_chsize function does the job better it get fd ans size to change to

sofr