Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wuarchive!uunet!rushpc!jjr From: jjr@rushpc (John J. Rushford Jr) Newsgroups: comp.lang.c Subject: Re: How do you truncate a file? Message-ID: <1991May12.155309.7279@rushpc> Date: 12 May 91 15:53:09 GMT References: <1991May6.194614.9641@pensoft.uucp> <792@uswnvg.UUCP> <1991May9.130725.1@dev8j.mdcbbs.com> Reply-To: jjr@rushpc.UUCP (John J. Rushford Jr) Distribution: usa Organization: My Place on the front Range Lines: 38 In article <1991May9.130725.1@dev8j.mdcbbs.com> campbell@dev8j.mdcbbs.com (Tim Campbell) writes: >In article <792@uswnvg.UUCP>, dfpedro@uswnvg.UUCP (Donn Pedro) writes: >> In article <1991May6.194614.9641@pensoft.uucp>, lwb@pensoft.uucp (Lance Bledsoe) writes: >> : In article fitz@mml0.meche.rpi.edu (Brian Fitzgerald) writes: >> : >Rudi Cilibrasi writes: >> : >>Suppose I have a 100-byte file called "myfile.dat", and I want to get >> : >>rid of the last 10 bytes. >> : > >> dfpedro@uswnvg.UUCP >-- >I've seen some interesting and somewhat amusing responses to this one so >far. > >IF you have DOS and want this to run real fast on any file of any size and >want it to truncate the file in place (not making a copy), then you could >always fseek to the 10th byte from the end of the file and overwrite and EOF >mark, then edit the directory entry in DOS to indicate that the file is now >10 bytes smaller. Of course you'd need to know the layout of a directory >entry and although the code would run faster compared to this example >(especially on big files), it would certainly require more time to write >the code. I'd only write it if I needed to do this often. Of course if >I didn't need to do this often then I'd probably just use a text editor. > >I just don't know unix well enough to know a clever way to handle this >situation without actually copying the file. > In Unix the above method would wreak havoc with the filesystem if you could use it. You would have to update the freelist and inode entry in the superblock or run fsck -y to fix the filesystem. This would prove to be difficult unless you're 'root'. -- John ---- Westminster Colorado |