
remove() vrs fopen("""w")
[ ... ]
Quote:
> I find for portability fopen("filename", "w") works better than remove(),
I can't think of any reason it would be more portable -- both fopen
and remove are required for a conforming implementation of C.
However, they do NOT do the same thing at all.
Quote:
> also if you happen to want to delete a file that may be a symbloic link
> without removing the link itself then fopen("filename", "w") followed by
> fclose() does the trick wonderfully.
Here you've got things exactly backwards. If you use fopen to
truncate the file, then the file itself is truncated, and ALL links
(whether hard or symbolic) will show the file as being truncated.
Worse yet, the link you started with will still exist, and will simply
refer to a file of zero length.
OTOH, if you use remove, it'll normally equate to (for example) unlink
on a UNIX system, so when you remove a link to the file, it only
affects that link, and all other links will see the file unchanged.
If you deal with a system where there's a 1:1 correspondence between
file names and files themselves (e.g. MS-DOS) you could argue that the
two are fairly similar. When you get to a system that supports
multiple links to a file (e.g. Windows NT or UNIX) that argument
becomes _completely_ specious and nonsensical. The two are doing
entirely different sorts of things.
I'm reasonably certain there's no such thing as a system that has
remove() remove the entire file regardless of how many links there
might be to it -- if nothing else, this would simply be a difficult,
time-consuming thing to do. As-is, you know where the link is, and
you remove that. You decrement the reference count in the file's own
entry (e.g. inode under UNIX) and if it's zero, you can remove the
file itself. However, to remove ALL links to the file, you'd
basically have to search the entire disk for links to the same file,
and remove all of them until the ref-count went to zero. At least in
the file systems of which I'm aware, there's no link from the file
entry itself back to each of the links, so there's no quick or
convenient method of finding all links to a file to remove them all.
--