Re: Setting the length of a file



"JosephKK"<quiettechblue@xxxxxxxxx> writes:
On Fri, 20 Mar 2009 00:32:30 -0700 (PDT), Old Wolf
<oldwolf@xxxxxxxxxxxxxx> wrote:

Is there any way to set the length of a file that
I've opened with fopen(filename, "r+b") ?
(I want to make the file shorter).

I want my code to work in both Unix and Windows;
in Unix there is a function ftruncate(); but I can't
find an equivalent function in either the C standard
or in my windows compiler.

I may be full of baloney or just thinking outside the box.

Open source file for read (rb) a temp file for write(wb), copy the
number of octets desired.
Close input file, delete input file, create new output file (wb) of
the same (path) name.
Reopen temp file (rb) and copy to output file.
Close both files.

Just might be portable.

Sure, but it could be extremely inefficient for large files, and it
could have undesirable side effects such as changing file metadata; it
can also fail if there's not enough room for a copy of the file. It's
very likely that both operating systems have system-specific methods
for truncating a file.

This is one of those tasks that can be done badly using pure standard
C, but much better using non-portable constructs.

Also, the steps of copying the temporary file back to the original
name can be accomplished using the standard C "rename" function.
Though rename() might be slow or impossible if, for examle, the two
files are not on the same filesystem -- and there's no completely
portable way to generate a name for the temporary file that's on the
same filesystem as the original.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Anyone have a Sandisk Extreme III card?
    ... If you live in a Windows world, ... UNIX and its variant need swapfile space and metadata space as well, ... and Windows had to do with file system block allocation. ... filesystem that could divide up a 2G disk very nicely. ...
    (rec.photo.digital)
  • Re: Questions about the behavior for argv(0)
    ... manner not compatible with Unix system configuration and/or ... the Windows programmer wishes to continue to ... configuration of a Unix standard environment. ... porting "platform specific" code is hard, ...
    (microsoft.public.vc.mfc)
  • Re: Makefile portability
    ... If you want interoperability you probably want a standard. ... it for writing applications where Windows is considered an important ... so they have to translate filenames which are passed between Cygwin/MSys ... read the file will be a "Unix" program or a Windows program. ...
    (comp.unix.programmer)
  • Re: Bad habits
    ... readis the system readfor the unix or unix like OS. ... should be in the "standard c" but in this case has the need of stdin. ... This wouldn't apply to stdin/stdout/stderr, as they're already open, and "framebuffer device" wouldn't apply to Windows at all... ... section _DATA public align=4 class=DATA use32 ...
    (alt.lang.asm)
  • Re: Windows RAID
    ... on previous versions of Windows NT. ... with a FAT32 file system into a Windows PC, ... and not mount the filesystems on it, ... It's available in Unix because that's how things were done in the earlier days of Unix, like 10 years ago, but even in Unix it's not absolutely necessary anymorel. ...
    (comp.sys.ibm.pc.hardware.storage)