how truncate a file in C?

how truncate a file in C?

Post by <- Chameleon -> » Tue, 17 Jun 2003 17:18:38



Hi!

I am extremely newbie in Linux, but now I am programming a library in C
which is cross platform.

My problem is this:

I want to truncate a file with size (e.g.) 50.000 bytes to size 500 bytes.
Unfortunatelly Standard C has not any function for file truncation and now I
use OS Specific calls with #preprocessor.

Which Linux call does file truncation? Which is the syntax? Which is header
file? Which is library?

In windows my code is:
------------------------
char mylib_truncate(FILE *fp, long size)
{
#ifdef WIN32

#include <io.h>
 return _chsize(_fileno(fp), size);        // Unfortunatelly this is
Microsoft Specific...

#else

// What must add here for file truncation?
// Linux Specific commands

#endif

Quote:}

--------------------------

If OS is MS Windows, WIN32 definition is set. With this definition compiler
knows that OS is MS Windows.
Which is the definition for Linux?

Thanks a lot

 
 
 

how truncate a file in C?

Post by DINH Viêt Ho » Tue, 17 Jun 2003 17:58:53


<- Chameleon -> wrote :

Quote:> I am extremely newbie in Linux, but now I am programming a library in C
> which is cross platform.

> My problem is this:

> I want to truncate a file with size (e.g.) 50.000 bytes to size 500 bytes.
> Unfortunatelly Standard C has not any function for file truncation and now I
> use OS Specific calls with #preprocessor.

> Which Linux call does file truncation? Which is the syntax? Which is header
> file? Which is library?

two functions are defined by one of the POSIX standard (IEEE Std 1003.1) :

       #include <unistd.h>

       int truncate(const char *path, off_t length);
       int ftruncate(int fd, off_t length);

(use the man luke to get more information)

to get the file descriptor from a FILE *, you can use fileno()

--
DINH V. Hoa,

"Elle a rien pour elle"

 
 
 

how truncate a file in C?

Post by Marc Rochkin » Tue, 17 Jun 2003 23:00:32




[snip]

Quote:

> to get the file descriptor from a FILE *, you can use fileno()

True, but truncating a file opened as a FILE would have an unpredictable
effect on further I/O with the FILE.

--Marc

 
 
 

how truncate a file in C?

Post by Valentin Nechaye » Wed, 18 Jun 2003 16:02:01


 >>to get the file descriptor from a FILE *, you can use fileno()
MR> True, but truncating a file opened as a FILE would have an unpredictable
MR> effect on further I/O with the FILE.

IMO fflush() before truncating and fseek() after it help from unpredictability.

-netch-

 
 
 

how truncate a file in C?

Post by Frank Cusac » Wed, 18 Jun 2003 17:24:33




>  >>to get the file descriptor from a FILE *, you can use fileno()
> MR> True, but truncating a file opened as a FILE would have an unpredictable
> MR> effect on further I/O with the FILE.

> IMO fflush() before truncating and fseek() after it help from unpredictability.

fflush() guarantees nothing here.  You cannot mix stdio and system I/O
predictably, period.  The effects of interchanging the calls are
system dependent, and since there is no guarantee, even on the same
system different things might happen at different times.

You don't want to write code that is documented as having undefined
behavior.

I would consider using stream = freopen(path, "w", stream).

That said, I'm sure you're correct.  You just shouldn't do that. :-)

/fc

 
 
 

how truncate a file in C?

Post by Valentin Nechaye » Wed, 18 Jun 2003 22:49:12



> MR>> True, but truncating a file opened as a FILE would have an unpredictable
> MR>> effect on further I/O with the FILE.
>> IMO fflush() before truncating and fseek() after it help from unpredictability.

FC> fflush() guarantees nothing here.

Citing SUSv3:

==={{{
fflush() shall cause any unwritten data for that stream to be written
to the file,
===}}}

FC>  You cannot mix stdio and system I/O
FC> predictably, period.

You didn't show any arguments for this words.

FC>  The effects of interchanging the calls are
FC> system dependent,

Where is it written? Please show.

-netch-

 
 
 

how truncate a file in C?

Post by Joe Halpi » Wed, 18 Jun 2003 23:16:53




> > MR>> True, but truncating a file opened as a FILE would have an unpredictable
> > MR>> effect on further I/O with the FILE.
> >> IMO fflush() before truncating and fseek() after it help from unpredictability.
> FC> fflush() guarantees nothing here.

> Citing SUSv3:

> ==={{{
> fflush() shall cause any unwritten data for that stream to be written
> to the file,
> ===}}}

> FC>  You cannot mix stdio and system I/O
> FC> predictably, period.

> You didn't show any arguments for this words.

> FC>  The effects of interchanging the calls are
> FC> system dependent,

> Where is it written? Please show.

SUS3 isn't quite as absolute as the above, but it does give some rules
for defined interaction between stdio streams and file descriptors,
see 'Interaction of File Descriptors and Standard I/O Streams" in
http://www.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_05...

If these rules aren't followed, the results are unspecified.

Joe

 
 
 

how truncate a file in C?

Post by Marc Rochkin » Thu, 19 Jun 2003 00:00:11


General response to this little thread:

The problem is completely solved if the FILE is closed before the truncaton
and then re-opened.

--Marc

 
 
 

how truncate a file in C?

Post by DINH Viêt Ho » Thu, 19 Jun 2003 02:08:15


Valentin Nechayev wrote :

Quote:> FC>  You cannot mix stdio and system I/O
> FC> predictably, period.

> You didn't show any arguments for this words.

> FC>  The effects of interchanging the calls are
> FC> system dependent,

> Where is it written? Please show.

You won't know what is done on fclose()
We can imagine that the file may be retruncated on fclose() for example.

--
DINH V. Hoa,

"T'aurais pas une question sur le langage C" -- Emmanuel Delahaye

 
 
 

how truncate a file in C?

Post by Valentin Nechaye » Thu, 19 Jun 2003 05:00:12



> FC>>  The effects of interchanging the calls are
> FC>> system dependent,
>> Where is it written? Please show.

JH> SUS3 isn't quite as absolute as the above, but it does give some rules
JH> for defined interaction between stdio streams and file descriptors,
JH> see 'Interaction of File Descriptors and Standard I/O Streams" in
JH> http://www.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_05...

Good link, it says all I knowed for the problem and some more ;))

JH> If these rules aren't followed, the results are unspecified.

And if followed, results are predictable and the same that wanted?

-netch-

 
 
 

how truncate a file in C?

Post by Frank Cusac » Thu, 19 Jun 2003 14:27:03





>> > MR>> True, but truncating a file opened as a FILE would have an unpredictable
>> > MR>> effect on further I/O with the FILE.
>> >> IMO fflush() before truncating and fseek() after it help from unpredictability.
>> FC> fflush() guarantees nothing here.

>> Citing SUSv3:

>> ==={{{
>> fflush() shall cause any unwritten data for that stream to be written
>> to the file,
>> ===}}}

>> FC>  You cannot mix stdio and system I/O
>> FC> predictably, period.

>> You didn't show any arguments for this words.

>> FC>  The effects of interchanging the calls are
>> FC> system dependent,

>> Where is it written? Please show.

> SUS3 isn't quite as absolute as the above, but it does give some rules
> for defined interaction between stdio streams and file descriptors,
> see 'Interaction of File Descriptors and Standard I/O Streams" in
> http://www.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_05...

I stand corrected!  However, for the simple case of truncating a file,
wow, I wouldn't want to convince myself that fflush() and rewind() are
*guaranteed* to work correctly with ftruncate() based on the SUSv3 doc.
I tried to read it to see what it says about this case, but couldn't
muster up the required concentration.  Note that there is a *huge*
difference between what you expect to work and what is guaranteed to
work.  Many an otherwise excellent programmer has gone down in flames
because of assumptions in this regard.

If freopen() weren't a viable option I would look at mixed solutions.
Does the freopen() solution seem to be deficient?

/fc

 
 
 

how truncate a file in C?

Post by Geoff Clar » Fri, 20 Jun 2003 21:38:04



>The problem is completely solved if the FILE is closed before the truncaton
>and then re-opened.

Doesn't seem like a very robust solution to me.
What if the file has been unlinked or renamed?

--

 
 
 

how truncate a file in C?

Post by Andrew Gabri » Wed, 25 Jun 2003 09:53:23




Quote:> <- Chameleon -> wrote :

>> I am extremely newbie in Linux, but now I am programming a library in C
>> which is cross platform.

>> My problem is this:

>> I want to truncate a file with size (e.g.) 50.000 bytes to size 500 bytes.
>> Unfortunatelly Standard C has not any function for file truncation and now I
>> use OS Specific calls with #preprocessor.

>> Which Linux call does file truncation? Which is the syntax? Which is header
>> file? Which is library?

> two functions are defined by one of the POSIX standard (IEEE Std 1003.1) :

>        #include <unistd.h>

>        int truncate(const char *path, off_t length);
>        int ftruncate(int fd, off_t length);

> (use the man luke to get more information)

You would do well to check the truncate worked, and have code to fall
back on a copy and rename if it didn't. The circumstances where it
might fail are where the filesystem holding the file doesn't support
it (e.g. some old unix or non-unix NFS server), even when the system
your application is running on does. Been there, got the T-shirt...

--
Andrew Gabriel
Consultant Software Engineer

 
 
 

1. Tie::File truncates large files when doing a shift

... check the changelog

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Michael J. Tobler: motorcyclist, surfer,  #    Black holes result
 skydiver, and author: "Inside Linux",     #   when God divides the  
 "C++ HowTo", "C++ Unleashed"              #     universe by zero

2. gs, printing

3. Truncating file from file descriptor

4. JRun vs mod_jserv for Apache servlets?

5. Truncating the file without closing the file.

6. FTP server help

7. truncated a file with "> file" but size comes back with "ls"

8. Looking for cheap and fast CPU

9. Netscape plug-ins, helper files, Acrobat etc.

10. Compare file modification date using tcsh built-ins

11. cd's files' names different and truncated

12. Recovering a truncated file

13. truncated file names