Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!pacbell.com!lll-winken!xanth!usenet From: kremer@tulip.cs.odu.edu (Lloyd Kremer) Newsgroups: comp.lang.c Subject: Re: How do you truncate a file? Message-ID: <1991May13.135202.6642@cs.odu.edu> Date: 13 May 91 13:52:02 GMT References: <1991May6.194614.9641@pensoft.uucp> <792@uswnvg.UUCP> <1991May9.130725.1@dev8j.mdcbbs.com> Sender: usenet@cs.odu.edu (Usenet News Poster) Distribution: usa Organization: Old Dominion University, Norfolk, VA Lines: 30 Nntp-Posting-Host: tulip.cs.odu.edu In article <1991May9.130725.1@dev8j.mdcbbs.com> campbell@dev8j.mdcbbs.com (Tim Campbell) writes: > >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. If you're running DOS, there is an "official" method to truncate files in-place. You open a file handle to the existing file, seek to the intended point of truncation, and perform a zero-length write. Caveat: Most C Compilers don't implement this in their write() function. You must use the real *DOS* function call, "write to open file handle," or whatever it's called. DOS C compilers provide a function like intdos() or bdos() to access DOS functions directly, calling them with the right register settings. I think it's DOS function 3D or 3E or around there (I don't have my DOS Technical Reference here). Since this is the legitimate method in DOS, it takes care of the directory entry and FAT automatically. P.S. This whole issue is not a C Language question; it's entirely an operating system concern. -- Lloyd Kremer Hilton Systems, Inc. Hampton, Virginia 23666