LINUX TIPS AND TRICKS --- August 24, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________

File Truncation
By Danny Kalev

This week, I will show how to truncate files using the 
truncate() and ftruncate() syscalls. 

The file system automatically expands a file's size as more 
data is appended to it; however, it has no way of knowing how 
to shrink a file when the data at its end is no longer needed. 
Consider the following scenario: You have a log file that 
contains a header at its beginning and a varying number of 
fixed-size records. You want to keep the log file for a certain 
amount of time (say up to a week after the last record was 
appended) and then shrink it to its original size (i.e., remove 
the records while keeping the header) so that new records will 
be appended after the header. One way to do this is copying the 
header to another file, delete the original file, and rename 
the copy. However, there is a simpler way to achieve the same 
effect. The <unistd.h> header declares two syscalls that 
truncate files, namely truncate() and ftruncate(). Here are 
their prototypes:

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

truncate() takes a filename as its first argument. It sets the 
file's size to length, discarding any trailing bytes past the 
new end. If length is larger than the current file size, then 
the file is expanded accordingly. The ftruncate() syscall is 
similar to truncate() except that it takes a file descriptor 
rather than a filename. Remember that when using ftruncate(), 
the file descriptor must refer to an open file.

In the following example, the program accepts a filename as a 
command line argument, displays its current size, prompts the 
user for a new size and truncates it accordingly:

    #include <sys/stat.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char * argv[])
    {
     struct stat info;
     size_t len;
     if(argc<2)
     {
      printf("filename not provided, aborting\n");
      exit(1);
     } 
     if (stat(argv[1], &info))
     {
      printf("stat() error, exit forced");
      exit(1);
     }
     printf("size of file %s is: %d \n", argv[1], info.st_size);
     printf("enter new size: \n")
     scanf("%d", &length);
     if (!truncate (argv[1], len))
     {
      printf("file %s was truncated successfully\n", argv[1]);
     }
     else
     {
      printf("truncation failed");
      exit(1);
     }
     return 0;
    }

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and 
distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ 
Professional Programmer's Handbook (Que, 1999). Danny can be 
reached at linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

How do I truncate a file?
http://www.faqs.org/faqs/unix-faq/faq/part3/section-3.html

truncate, truncate64, ftruncate, or ftruncate64 Subroutine
http://nim.cit.cornell.edu/usr/share/man/info/en_US/
a_doc_lib/libs/basetrf2/truncate.htm

Linux Tips and Tricks Newsletter Archive
http://www.itworld.com/lnx_tip/
________________________________________________________________


ITWORLD.COM SERVICES

ITWorld.com's RFP Exchange is the place to go for your 
outsourcing needs

Post an RFP for FREE and receive proposals from qualified IT 
providers.
Go to the RFP Exchange and get your projects started today!
http://itworld.newmediary.com/itw0608nwsltrb
________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://www.itworld.com/newsletters
- Click on "View my newsletters" to log in and manage your
  account
- To subscribe, check the box next to the newsletter
- To unsubscribe, uncheck the box next to the newsletter 
- When finished, click submit

Questions? Please e-mail customer service at: mailto:
support@itworld.com
________________________________________________________________

CONTACTS

* Editorial: Andrew Santosusso, Associate Editor, Newsletters, 
  andrew_santosusso@itworld.com

* Advertising: Clare O'Brien, Vice President of Sales, 
  clare_obrien@itworld.com

* Recruitment advertising: Jamie Swartz, Eastern, Regional
  Sales Manager, jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager, paul_duthie@itworld.com

* Other inquiries: Jodie Naze, Senior Product Marketing Manager, 
  jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
